ngChange is called when model changed programmatically

前端 未结 4 1634
旧时难觅i
旧时难觅i 2021-02-08 06:15

I have a problem when angular\'s ng-change is called when model is changed programmatically.

$scope.sendMessage = function() {
    $scope.message = \"Message sen         


        
4条回答
  •  盖世英雄少女心
    2021-02-08 06:57

    According to docs, you're right.

    https://docs.angularjs.org/api/ng/directive/ngChange

    but this seems to be a bug caused by the order in which the events are hooked up

    The best way round it - with resorting to js handler (onchange)

    $scope.$watch("mySelectBox", function(a,b) {
        if (a.name!=b.name) {
           $scope.message = "Message sent! (old="+b.name+', new='+a.name+')';
        }
      });
    

    See plunk http://plnkr.co/edit/2ZbxS1tszppR9SrNqxVB?p=preview

    HTH

提交回复
热议问题