I have a structure which looks like this: http://jsfiddle.net/deeptechtons/TKVH6/
<div>
<ul ng-controller="checkboxController">
<li>Check All
<input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" />
</li>
<li ng-repeat="item in Items">
<label>{{item.Name}}
<input type="checkbox" ng-model="item.Selected" />
</label>
</li>
</ul>
</div>
angular.module("CheckAllModule", [])
.controller("checkboxController", function checkboxController($scope) {
$scope.Items = [{
Name: "Item one"
}, {
Name: "Item two"
}, {
Name: "Item three"
}];
$scope.checkAll = function () {
if ($scope.selectedAll) {
$scope.selectedAll = true;
} else {
$scope.selectedAll = false;
}
angular.forEach($scope.Items, function (item) {
item.Selected = $scope.selectedAll;
});
};
});
When check all is selected or deselected, all other checkboxes are selected. But when "Check All" is selected, and I deselect one of the items, I want "Check All" to be deselected.
Thanks in advance!
(PS: Thanks to deeptechtons for the JSFiddle)
Here is a neat little way of doing this
http://jsfiddle.net/TKVH6/850/
Use a little bit of undescore (just the reduce) and ng-checked
You could use javascripts built in .reduce if you did not want to use underscore.
You can use this function to check if all records are checked whenever a checkbox changes:
The view code:
http://jsfiddle.net/TKVH6/840/
This answer has similar logic to Kmart2k1's answer. It puts the responsibility of updating the master checkbox on each child in the
ng-repeat
. Instead of usingarray.forEach
, I usearray.some
to more quickly check if any of the items are unselected.HTML:
Javascript
Forked fiddle
If you are using Angular 1.3+ you can use
getterSetter
fromng-model-options
to solve this and avoid manually keeping track of theallSelected
stateHTML:
JS:
http://jsfiddle.net/2jm6x4co/
working example: http://jsfiddle.net/egrendon/TKVH6/845/
view:
controller: