JOHN MAINA
JOHN MAINA

Reputation: 23

Getting the checked checkbox on ngrepeat angularjs

I am using ng-repeat in angularjs to display several checkboxes.

<div class="checkbox" ng-repeat="subject in priceinformation.subjects">
   <label>
       <input type="checkbox" ng-model="priceinformation.subscriptioninfo.subject.name"> {{subject.name}}
   </label>
</div>

The problem is all the checkboxes are using the same model, hence if I check one, all are checked. I want to do it in a way that the model can resolve to different values. e.g.

ng-model="priceinformation.subscriptioninfo.subject.{{subject.name}}"

{{subject.name}} should resolve to something like English, History etc, hence a different model for each checkbox. But ng-model="priceinformation.subscriptioninfo.subject.{{subject.name}}" is giving an error.

Note: {{subject.name}} is resolving correctly used elsewhere, and 'priceinformation.subscriptioninfo.subject' is working correctly.

Upvotes: 0

Views: 161

Answers (2)

Coder
Coder

Reputation: 7076

Use [] instead of .

ng-model="priceinformation.subscriptioninfo[subject.name]"

Plnkr Demo

Script

$scope.priceinformation ={};

  $scope.priceinformation.subjects = [{"name":"English"},{"name":"History"},{"name":"Maths"}];

  $scope.priceinformation.subscriptioninfo ={};

Upvotes: 1

rmuller
rmuller

Reputation: 1837

<div ng-repeat="item in checks">
    <input type="checkbox" ng-model="item.checked">{{item.name}}
</div>

See Plunkr

Upvotes: 0

Related Questions