Book Image

AngularJS Web application development Cookbook

By : Matthew Frisbie
Book Image

AngularJS Web application development Cookbook

By: Matthew Frisbie

Overview of this book

Packed with easy-to-follow recipes, this practical guide will show you how to unleash the full might of the AngularJS framework. Skip straight to practical solutions and quick, functional answers to your problems without hand-holding or slogging through the basics. Avoid antipatterns and pitfalls, and squeeze the maximum amount out of the most powerful parts of the framework, from creating promise-driven applications to building an extensible event bus. Throughout, take advantage of a clear problem-solving approach that offers code samples and explanations of components you should be using in your production applications.
Table of Contents (17 chapters)
AngularJS Web Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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