For the $watch in last article, we watch 'user.password', actually it is a string.

If you watch 'user', this object and we do something like:

function WatchCtrl ($scope) {

    $scope.$watch('user', function (newVal, oldVal) {
        console.log(newVal, oldVal);
    });

}

 

Actually it won't work.

Because, $watch without the third param is a reference watching. If we add the third param:

function WatchCtrl ($scope) {

    $scope.$watch('user', function (newVal, oldVal) {
        console.log(newVal, oldVal);
    }, true);

}

Then it lost value watching, but it is qiute expensive.

 

Actually we have another way to do that if what we are watching is an object by using $watchCollection:

function WatchCtrl ($scope) {

    $scope.$watchCollection('user', function (newVal, oldVal) {
        console.log(newVal, oldVal);
    });

}

 

相关文章:

  • 2021-09-22
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
猜你喜欢
  • 2021-12-16
  • 2021-05-12
  • 2021-05-17
  • 2021-10-24
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案