athi
athi

Reputation: 123

ng-repeat loop on checkboxes

  <span  ng-repeat="s  in colors">
         <p><li><input type="checkbox"   ng-model="colors[id].checked"   ng-change="setColor(s.id)"> &nbsp; {{s.name}}</li></p> 
</span> 

In my controller,

$scope.colorsel= []; 
    $scope.setColor = function(a){
         if($scope.colorsel.indexOf(a) == -1 ){
              $scope.colorsel.push(a);
         }
         else{
             var index =  $scope.colorsel.indexOf(a)
            $scope.colorsel.splice(index,1);

         }
    }

Am getting the colorsel values correctly. But if i select one checkbox, it is just showing that all checkboxes are selected(means tick mark appears in all checkboxes but it is taking only the id of the check box which is selected in colorsel array). I f I select other one, all the tick marks will disappear. Plz help me to solve this.

Upvotes: 4

Views: 300

Answers (3)

D Mishra
D Mishra

Reputation: 1578

ng-repeat="s in colors" is similar as foreach(s in colors) So if you want to access values from colors you need to use "S" inplace of colors.

Upvotes: 0

S. Divya
S. Divya

Reputation: 91

Try this

<span  ng-repeat="s in colors">
 <p>
  <input type="checkbox" ng-model="s[id].checked" ng-change="setColor(s.id)"> &nbsp; {{s.name}}
 </p> 
</span> 

Upvotes: 0

Alon Eitan
Alon Eitan

Reputation: 12025

Just reference directly to the object: ng-model="s.checked".

This: colors[id].checked is wrong because you reference to the same id (Perhaps s.id colors[s.id].checked is what you're trying). So if you have in your controller $scope.id = 2 than you always referencing to colors[2].checked.

Upvotes: 1

Related Questions