add watch on a non scope variable in angularjs

后端 未结 3 1144
自闭症患者
自闭症患者 2021-02-01 06:10

Is there a way to add watch to a non scope variable. I want to add a watch to local variable. I have something like this

 function EditAssetRegistryController(as         


        
3条回答
  •  灰色年华
    2021-02-01 06:31

    $watch will not work as normal syntax with controllerAs. You need to bind it to $scope, and then you can watch that variable:

    Code

    $scope.$watch(angular.bind(this, function (ch_group_uniq) {
      return this.ch_group_uniq;
    }), function (newVal, oldVal) {
      console.log('Name changed to ' + newVal);
    });
    

    Here is the reference Todd Motto Article

提交回复
热议问题