加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:09:52 所属栏目:安全 来源:网络整理
导读:点击复选框并调用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:在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
– http://jsfiddle.net/supercobra/ekD3r/

工作fidddle w / Angular 1.0.0
– 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读