单击复选框并调用 ng-click:在 ng-click 启动之前模型未更新,因此复选框值错误地显示在 UI 中:
这在 AngularJS 1.0.7 中有效,在 Angular 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;
};
}]);
Broken Fiddle w/Angular 1.2 RCx - http://jsfiddle.net/supercobra/ekD3r/
带 Angular 1.0.0 的工作小提琴 - http://jsfiddle.net/supercobra/8FQNw/