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

前端 未结 10 1665
太阳男子
太阳男子 2020-12-04 13:17

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 An

10条回答
  •  爱一瞬间的悲伤
    2020-12-04 13:42

    The ordering between ng-model and ng-click seems to be different and it's something you probably shouldn't rely on. Instead you could do something like this:

  • {{todo.text}} {{todo.done}}

  • task: {{current.text}}

    Wrong value

    done: {{current.done}}

    And your script:

    angular.module('myApp', [])
        .controller('Ctrl', ['$scope', function($scope) {
    
            $scope.todos=[
                {'text': "get milk",
                 'done': true
                 },
                {'text': "get milk2",
                 'done': false
                 }
                ];
    
            $scope.current = $scope.todos[0];
    
    
           $scope.onCompleteTodo = function(todo) {
                console.log("onCompleteTodo -done: " + todo.done + " : " + todo.text);
        //$scope.doneAfterClick=todo.done;
        //$scope.todoText = todo.text;
           $scope.current = todo;
    
       };
    }]);
    

    What's different here is whenever you click a box, it sets that box as what's "current" and then display those values in the view. http://jsfiddle.net/QeR7y/

提交回复
热议问题