Tấn Đức Nguyễn Tấn Đức Nguyễn - 10 months ago 54
AngularJS Question

How can change $scope value in ng-repeat?

I have div ng-repeat:

<div ng-repeat="a in list" ng-click="abc = !abc">
//some code here
<input type="checkbox" ng-model="abc">
<button ng-click="reset()"> reset </button>

I use ng-model pass value to check/uncheck checkbox.
I have a button, when click it, all checkbox uncheck. I use $scope.abc=false; but nothing change. How can i change ng-model value ?

Answer Source

Your "abc" variable is a global one. You have to store the status as a property for every single object like already suggested.

Here a working example:


  <div ng-repeat="item in list" ng-click="item.checked = !item.checked">
     <input type="checkbox" ng-model="item.checked">{{item.value}} (clickme!)
  <button ng-click="reset()"> reset </button>



var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.list = [{value: 1}, {value: 2}, {value: 3}];
    var length = $scope.list.length;
    $scope.reset = reset;

    function reset() {

      for (var i=0; i<length; i++) {
       var item = $scope.list[i];
       item.checked = false;

(you can see it in action here)