点击复选框并调用ng-click:在ng-click启动之前,模型不会更新,因此在UI中错误地显示了复选框值:
这在AngularJS 1.0.7中工作,似乎在Angualar 1.2-RCx中断了。
<div ng-app="myApp" ng-controller="Ctrl"> <li ng-repeat="todo in todos"> <input type='checkBox' ng-click='onCompleteTodo(todo)' ng-model="todo.done"> {{todo.text}} </li> <hr> task: {{todoText}} <hr><h2>Wrong value</h2> done: {{doneAfterClick}}
和控制器:
angular.module('myApp',[]) .controller('Ctrl',['$scope',function($scope) { $scope.todos=[ {'text': "get milk",'done': true },{'text': "get milk2",'done': false } ]; $scope.onCompleteTodo = function(todo) { console.log("onCompleteTodo -done: " + todo.done + " : " + todo.text); $scope.doneAfterClick=todo.done; $scope.todoText = todo.text; }; }]);
破碎的小提琴w /角1.2 RCx@H_301_10@ – http://jsfiddle.net/supercobra/ekD3r/
工作fidddle w / Angular 1.0.0@H_301_10@ – http://jsfiddle.net/supercobra/8FQNw/
如何改变
原文链接:https://www.f2er.com/angularjs/146951.html<input type='checkBox' ng-click='onCompleteTodo(todo)' ng-model="todo.done">
至
<input type='checkBox' ng-change='onCompleteTodo(todo)' ng-model="todo.done">
从docs:
Evaluate given expression when user changes the input. The expression is not evaluated when the value change is coming from the model.
Note,this directive requires
ngModel
to be present.