supercobra supercobra - 1 year ago 94
AngularJS Question

Clicking a checkbox with ng-click does not update the model

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:

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">
task: {{todoText}}
<hr><h2>Wrong value</h2>
done: {{doneAfterClick}}

and controller:

angular.module('myApp', [])
.controller('Ctrl', ['$scope', function($scope) {
{'text': "get milk",
'done': true
{'text': "get milk2",
'done': false

$scope.onCompleteTodo = function(todo) {
console.log("onCompleteTodo -done: " + todo.done + " : " + todo.text);
$scope.todoText = todo.text;


Broken Fiddle w/ Angular 1.2 RCx

Working fidddle w/ Angular 1.0.0

Answer Source

How about changing

<input type='checkbox' ng-click='onCompleteTodo(todo)' ng-model="todo.done">


<input type='checkbox' ng-change='onCompleteTodo(todo)' ng-model="todo.done">

From 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download