2016-07-18 12 views
1

をAngularJS。この機能はすべてのアイテムを選択するためにのみ実装されています。また、すべてのアイテムの選択を解除するようにアイテムを変更することもできます。選択/選択解除し、すべてのチェックボックス要素は、私がチェックボックスとAngularJS内の項目のリストを持っていると私はすべての項目を選択することができますチェックボックスを持っている

これは私の関数である:

$scope.items.allItemsSelected = false; 
$scope.selectAll = function() { 
    for (var i = 0; i < $scope.itemsList.length; i++) { 
     $scope.temp.push($scope.itemsList[i].name); 
     console.log($scope.itemsList[i].name); 
     $scope.itemsList[i].isChecked = $scope.items.allItemsSelected; 
    } 
}; 

は、これは私のhtmlです:

<md-checkbox ng-model="items.allItemsSelected" 
      ng-change="selectAll()"> 
    Select all 
</md-checkbox> 

誰も要素の選択を解除するために、これを変更する方法についてのアイデアを持っていますか?

ありがとうございます!

+0

あなたは、配列 '$ scope.itemsList'との例を提供することができますHTMLの 'ngRepeat'ですか? https://docs.angularjs.org/api/ng/directive/ngChecked –

答えて

0

使用NG-チェックを属性。

<md-checkbox ng-model="items.allItemsSelected" ng-change="selectAll()"> 
    Select all 
</md-checkbox> 

checkbox1--

<md-checkbox ng-checked="isSelectAll" ng-model="items.allItemsSelected" ng-change="selectAll()"> 
    Select all 
</md-checkbox> 

checkbox2--

<md-checkbox ng-checked="isSelectAll" ng-model="items.allItemsSelected" ng-change="selectAll()"> 
    Select all 
</md-checkbox> 

JS:

$scope.isSelectAll = false; 
$scope.selectAll = function() { 
    if (!$scope.isSelectAll) { 
     $scope.isSelectAll = true; 
    } else { 
     $scope.isSelectAll = false; 
    } 
} 
+1

注意私の配列を空にする –

+0

私は私がしたいすべての項目の選択を解除するとき:これは予期しない動作につながる可能性として 'ngChecked'ディレクティブは、ngModel''と一緒に使用してはならないことを – blaa

+0

allを選択解除すると、配列$ scope.itemsList = []をselectAll関数のelse条件に入れます。 –

関連する問題