I have a selected array which will hold all the items that are selected. So when you change a checkbox the item gets added or removed from that array. Problem is that if something else changes the selected array the checkboxes don't update.
Storing the selected state on the item it self isn't an option since in my app the items array gets repopulated depending on the url. So the selected array is a global array holding all the items you have selected. To give it some context my app is a file browser and you can select files in different directories.
Plunker
http://plnkr.co/edit/h9t3caBzk7hyTYJysCay?p=preview
js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.selected = ['one'];
$scope.items = ['one', 'two', 'three'];
$scope.select = function (item, s) {
if (s) {
return $scope.selected.push(item);
}
$scope.selected.splice($scope.selected.indexOf(item.path), 1);
}
});
app.controller('ItemCtrl', function ($scope) {
$scope.s = $scope.selected.some(function (i){
return i === $scope.item;
})
})
Html
<body ng-controller="MainCtrl">
<button ng-click='selected.length = 0'>Clear selected</button>
<p ng-repeat='item in items' ng-controller='ItemCtrl'>
<input type='checkbox' ng-change='select(item, s)' ng-model='s' /> {{item}}
</p>
{{selected}}
</body>