Optimizing the application using equality $watch
Equality watches register a listener that uses angular.equals()
as the comparator, which exhaustively examines the entirety of all objects to ensure that their respective object hierarchies are identical. Both a new object assignment and property modification will register as a change and invoke the watch callback.
This watcher should be used when any modification to an object is considered as a change event, such as a user object having its properties at various depths modified.
How to do it…
The equality comparator is used when the optional Boolean third argument is set to true
. Other than that, these watchers are syntactically identical to reference comparator watchers, as shown here:
$scope.myObj = {
myPrim: 'Go Bears!',
myArr: [3,1,4,1,5,9]
};
// watch myObj by equality
$scope.$watch('myObj', function(newVal, oldVal, scope) {
// callback logic
}, true);
How it works…
The equality comparator will invoke the watch callback on every modification...