0

すべての子コントローラーを通過し、それらの関数を実行する for ループを作成したいと考えています。現時点では、コードは子コントローラーの 1 つのインスタンスでのみ関数を実行しています。ng-repeat で生成されたコントローラーのすべてのインスタンスを反復するにはどうすればよいですか?

基本的に、ボタンをクリックすると、「wordlistSets」の各「セット」で生成された各コントローラーに対して 1 つずつ、childController の関数を 4 回実行する必要があります。

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>

角度コントローラー:

myApp.controller("parentController", function ($scope) {
   $scope.setAll = function(value) {
       var index;
       for (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;
    for (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;
    for (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 from array
    } else {
        $scope.checkedWordlists.push(wordlist);//add to 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 to array
    } else if (!parentChecked && index > -1) {//is in array
        $scope.checkedWordlists.splice(index, 1);//remove from array
    }
};

});
4

1 に答える 1