Optimizing the application using reference $watch
Reference watches register a listener that uses strict equality (===
) as the comparator, which verifies the congruent object identity or primitive equality. The implication of this is that a change will only be registered if the model the watcher is listening to is assigned to a new object.
How to do it…
The reference watcher should be used when the object's properties are unimportant. It is the most efficient of the $watch
types as it only demands top-level object comparison.
The watcher can be created as follows:
$scope.myObj = { myPrim: 'Go Bears!', myArr: [3,1,4,1,5,9] }; // watch myObj by reference $scope.$watch('myObj', function(newVal, oldVal, scope) { // callback logic }); // watch only the myPrim property of myObj by reference $scope.$watch('myObj.myPrim', function(newVal, oldVal, scope) { // callback logic }); // watch only the second element of myObj.myArr by reference $scope.$watch('myObj.myArr[1]', function(newVal, oldVal...