samedi 21 février 2015

Simple check all not working in AngularJS

I am trying to include a check all for my todo list, but once I hit toggleAll() it gets unchecked right away, plus it unchecks whichever one is already checked.


I really couldn't figure out why this behavior, perhaps some other things in the page are interfearing. A simple version of what I have included here is in this jsfiddle http://ift.tt/17mUYW8 and it is working.



<section id="main">
<input id="toggle-all" type="checkbox" ng-model="selectedAll" ng-click="toggleAll()">
<label for="toggle-all">Mark all as complete</label>
<ul id="todo-list">
<li ng-repeat="todo in todos" ng-class="{'completed': todo.isCompleted=='true'}" class="editing">
<div class="view" >
<input class="toggle" type="checkbox" ng-click="complete(todo)" ng-model="todo.isCompleted" ng-checked="{{todo.isCompleted}}" ng-true-value="'true'" ng-false-value="'false'">
<label ng-hide="isEditing" ng-dblclick="isEditing = !isEditing">{{todo.title}}</label>
<button class="destroy" ng-click="remove(todo)"></button>
</div>
<input class="edit" ng-show="isEditing" ng-model="todo.title" ng-blur="isEditing = !isEditing;edit(todo);">
</li>
</ul>
</section>


Controller



$scope.toggleAll = function () {
if ($scope.selectedAll) {
$scope.selectedAll = true;
} else {
$scope.selectedAll = false;
}
angular.forEach($scope.todos, function (todo) {
todo.isCompleted = $scope.selectedAll;
});
};


Any help is appreciated.





Aucun commentaire:

Enregistrer un commentaire