I have this fiddle: http://jsfiddle.net/j3ca/3kpoL6vg/3/
HTML:
<div ng-app>
<div ng-controller="MainCtrl">
<div class="item" ng-repeat="item in items" ng-class="{'active': item.active}" ng-click="item.active = !item.active">{{ item.name }}</div>
<button ng-click="mark()" ng-class="{'marked': allMarked}">Mark all</button>
</div>
JS:
function MainCtrl($scope){
$scope.items = [
{ id: 1, name: 'Item 1', active: false },
{ id: 2, name: 'Item 2', active: false },
{ id: 3, name: 'Item 3', active: false },
{ id: 4, name: 'Item 4', active: false }
];
$scope.allMarked = false;
$scope.mark = function() {
for(var i = 0; i < $scope.items.length; i++) {
if($scope.allMarked) {
$scope.items[i].active = false;
} else {
$scope.items[i].active = true;
}
}
$scope.allMarked = !$scope.allMarked;
}
}
I have the following functionalities:
- click on a list item changes "active" property of an item and sets its background to green if active == true;
- click on Mark all button sets all list elements to "active" and changes color of the button;
What I would like to do is this:
- If I manually select all items in the list the button also changes color to green;
- If I click on Mark all button, but then I uncheck one item manually, the button should change its color back to red (since not all items are selected);
What I need is this basically: http://plnkr.co/edit/N98IKTcHoZMCs18FjSRF?p=preview But not with checkboxes, I need divs.
My idea is: every time i click on a link, I check (for loop) if all other items are active, but that seems like a lot of unnecessary looping :)
Is there a way in angular to determine (on click) if all items in a list have the same value of a certain property?
I am new to Angular, help would be much appreciated :)
Updated fiddle
I've updated my fiddle link, but that is clearly a terrible terrible solution :) Any ideas? Is there a way to check item's active value without looping on every click?