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 ...

Get AngularJS Web Application Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.