angularjs - How can you iterate through controllers in an ng-repeat from the parent? -
i want write loop goes through of child controllers , runs function in them. @ moment code running function in 1 instance of child controller. how iterate through instances of controller, generated through ng-repeat?
basically, on click of button function in childcontroller should run 4 times, 1 each controller generated on each 'set' in 'wordlistsets'.
html:
<button ng-click="setall(true)">select all</button><button ng-click="setall(false)">deselectall</button> <ul> <li ng-repeat="set in wordlistsets" ng-controller="setcontroller"> <div class="parentselector"> <input type="checkbox" ng-click="selectallchildren(set.content)" ng-checked="parentchecked"> </div> <div ng-repeat="wordlist in set.content" ng-click="togglecheckbox(wordlist)"> <input type="checkbox" ng-checked="checkedwordlists.indexof(wordlist)> -1"> </div> </li> </ul>
angular controllers:
myapp.controller("parentcontroller", function ($scope) { $scope.setall = function(value) { var index; (index in $scope.wordlistsets) { $scope.setall.selectallchildren($scope.wordlistsets[index].content, value); } }; }); myapp.controller("childcontroller", function ($scope) { $scope.parentchecked = false; $scope.checkedwordlists = []; $scope.selectallchildren = function(wordlists) { if ($scope.parentchecked == false) { $scope.parentchecked = true; } else { $scope.parentchecked = false; } var index; (index in wordlists) { $scope.setchild(wordlists[index], $scope.parentchecked); } }; $scope.setall.selectallchildrenvalue = function(wordlists, value) { if (value == false && $scope.parentchecked == true) { $scope.parentchecked = false; } else if (value == true && $scope.parentchecked == false) { $scope.parentchecked = true; } var index; (index in wordlists) { $scope.setchild(wordlists[index], $scope.parentchecked); } }; $scope.togglecheckbox = function(wordlist) { var index = $scope.checkedwordlists.indexof(wordlist); if (index > -1) {//is in array $scope.checkedwordlists.splice(index, 1);//remove array } else { $scope.checkedwordlists.push(wordlist);//add array } }; $scope.setchild = function(wordlist, parentchecked) { var index = $scope.checkedwordlists.indexof(wordlist); if (parentchecked && index <= -1) {//is not in array $scope.checkedwordlists.push(wordlist);//add array } else if (!parentchecked && index > -1) {//is in array $scope.checkedwordlists.splice(index, 1);//remove array } }; });
i know didn't many answers made me realize in fact, thinking backwards. set "checked" property in json data , set ng-checked. setall function, iterated through json foreach , set each .checked value = true/false. :)
Comments
Post a Comment