Good day! There is a conclusion in the cycle

<div ng-repeat="item in items"> <p>{{ item.title }}</p> <input type="checkbox" ng-model="checkboxmodel" ng-change="checkboxmodel==1? func1(item) : func2(item)" ng-true-value="1" ng-false-value="0" /> </div> 

Those. for each item must be your checkbox. Its value is not contained in the item, but is additionally calculated. With checked / unchecked, everything works, the functions func1(item), func2(item) worked out. But how to set the initial value of the checkbox? Now the model is one for all checkboxes, and there is no possibility to set the value of a specific checkbox through $scope.checkboxmodel

Trying to do through arrays:

 <div ng-repeat="item in items"> <p>{{ item.title }}</p> <input type="checkbox" ng-model="ItemsCtrl.checkboxes[$index]" ng-change="ItemsCtrl.checkboxes[$index]==1? func1(item) : func2(item)" ng-true-value="1" ng-false-value="0" /> </div> 

In the ItemsCtrl controller, where items are added, added:

 $scope.checkboxes = []; 

Then I try to add a new item - a checkbox with a value of 1:

 $scope.checkboxes.push(1); 

But, of course, does not work. Tell me please.

    1 answer 1

    This is how it should work:

     <div ng-repeat="item in items"> <p>{{ item.title }}</p> <input type="checkbox" ng-model="checkboxes[$index]" ng-change="checkboxes[$index]==1? func1(item) : func2(item)" ng-true-value="1" ng-false-value="0" /> </div> 

    those. The $ scope of the controller must be available without a specific controller.

    But instead of ng-change it’s better to use the control in the controller, otherwise do not forget to share in $scope func1 and func2

    • Thank you so much, it worked. - Emm
    • Please explain about sharing func1 and func2 - Emm
    • Suppose my func1 (item) in the ItemsCtrl controller looks like this: $ scope.func1 = function (item) {...}. How can I call it now from ng-change? - Emm
    • $scope - the scope of the template, if you use any functions defined in the controller, they must be made $scope properties, if this is not done yet, similarly as you do with items , i.e. $ scope.func1 = function () ... - 11111000000
    • I have functions right now like $ scope properties - Emm