1

Here is my code on HTML. I find it on google that I can use this.

<input type="checkbox" ng-true-value="addTitle(cpPortfolioItem)" ng-false-value="removeTitle(cpPortfolioItem)">

Here is my angularJS controller.

$scope.addTitle = function(cpPortfolioItem){
        $scope.selectedTitles.push(cpPortfolioItem.id);
        console.log('$scope.selectedTitles', $scope.selectedTitles);
     };

     $scope.removeTitle = function(cpPortfolioItem){
        $scope.selectedTitles.splice(cpPortfolioItem.id,1);
        console.log('$scope.selectedTitles', $scope.selectedTitles);
     };

it doesn't work. I have logged it in console but I can see it neither push or splice the array. Maybe ng-true-value is not a valid directive? Anyone can help me on this? I will really appreciate it.

4
  • Use ng-checked attribute instead of true & false Commented May 12, 2015 at 7:24
  • how to tell if it is unchecked? Commented May 12, 2015 at 7:26
  • try stackoverflow.com/questions/22373562/pass-function-in-ng-model Commented May 12, 2015 at 7:29
  • ng-checked is an attribute and ngChecked is a boolean, returning true if it's checked, false if it isn't. You can use both ways ( from API ngChecked) Commented May 12, 2015 at 7:30

2 Answers 2

2

Base on the documentation ng-true-value and ng-false-value value are not event handlers. These are the value set to the model when input is checked(ng-true-value) or unchecked(ng-false-value).

Use this instead or use ng-model and attach $watch to the model.

Sign up to request clarification or add additional context in comments.

Comments

0

Use ng-change

<input type="checkbox" ng-change="titleChange()" ng-model="titleChanged">

In controller,

$scope.titleChanged = false;
$scope.titleChange = function() {
   if ($scope.titleChanged) {
      $scope.addTitle(cpPortfolioItem);
   } else {
      $scope.removeTitle(cpPortfolioItem);
   }
}

Working example:- http://jsfiddle.net/Shital_D/Lcumc3t7/10/

2 Comments

it doesn't work. it says ReferenceError: titleChanged is not defined
You need to add $scope.titleChanged in your controller. And assigned true or false value to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.