angularjs – 点击ng-click的复选框不会更新模型

前端之家收集整理的这篇文章主要介绍了angularjs – 点击ng-click的复选框不会更新模型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
点击复选框并调用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/

如何改变
<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.

原文链接:https://www.f2er.com/angularjs/146951.html

猜你在找的Angularjs相关文章