SJMan
SJMan

Reputation: 1607

Binding checkbox to model and check/unchecking it in Angular

I have a the following HTML:

<li ng-repeat="test in lists.Tests">
    <label>
        <input type="checkbox" id="cbxAcceptTest" ng-checked="{{test.IsAccepted}}"  ng-click="testSelection($event,{{test}})" />
    </label>
</li>

and the testSelection method as :

$scope.testSelection = function ($event, test) {
    if (test.IsAccepted) {
        alert('accept');
    }
    else {
        alert('reject');
    }

};

I am not able to trigger this method on ngClick, It works on ngChecked, but then I have to use ngModel, which Ive read does not go with ngChecked in the following link.

Upvotes: 3

Views: 138

Answers (3)

Please check this Plnkr.

In HTML you need remove your curly braces

  <li ng-repeat="test in lists.Tests">
    <label>
        <input type="checkbox" id="cbxAcceptTest" ng-checked="{{test.IsAccepted}}"  ng-click="testSelection(test)" />
    </label>

Upvotes: 1

cloudberry
cloudberry

Reputation: 369

Just remove the curly braces around test when you pass it as a parameter.

Upvotes: 1

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

In my case I have something like this:

<li ng-repeat="item in items">
    <label>
        <input type="checkbox" ng-model="item.auto" ng-click="onAutoClick(item)" />
    </label>
</li>

In my controller:

$scope.onAutoClick = function(item) {
  if(item.auto){
     // checked (true condition)
  } else  {
     // unchecked ( false condition)
  }

};

Upvotes: 1

Related Questions