点击与NG-点击复选框不更新模型 [英] Clicking a checkbox with ng-click does not update the model

查看:152
本文介绍了点击与NG-点击复选框不更新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击复选框,并要求NG-点击:不更新模型前NG-点击踢这样的复选框值是错误地在UI psented $ P $:

Clicking on a checkbox and calling ng-click: the model is not updated before ng-click kicks in so the checkbox value is wrongly presented in the UI:

这工作在AngularJS 1.0.7和Angualar 1.2 RCX似乎打破了。

This works in AngularJS 1.0.7 and seems broken in 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;

   };
}]);

破碎小提琴瓦特/ 1.2角RCX
  - http://jsfiddle.net/supercobra/ekD3r/

工作fidddle W /角1.0.0
  - http://jsfiddle.net/supercobra/8FQNw/

Working 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">

通过文档

当用户改变输入给出评估前pression。当值的变化是从模型来恩pression不计算。

Evaluate given expression when user changes the input. The expression is not evaluated when the value change is coming from the model.

请注意,该指令要求 ngModel 是present。

Note, this directive requires ngModel to be present.

这篇关于点击与NG-点击复选框不更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆