angularjs $watch old value and new value are the same

后端 未结 4 1097
忘了有多久
忘了有多久 2020-12-30 20:29

My intention is to watch a model within scope, and find difference between old value and new value.

However, I found old value and new value are all the same from th

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 21:17

    I found that it's very useful to check if the new and old value are equal (in values) and skip the process if that's the case to avoid unexpected behavior. You can use angular.equals to achieve that. Here's an example:

    JS:

    $scope.$watch('myObject', function(newValue, oldValue){
        if(angular.equals(newValue, oldValue)){
            return; // simply skip that
        }
    });
    

提交回复
热议问题