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 $watchCollection


AngularJS offers the $watchCollection intermediate watch type to register a listener that utilizes a shallow watch depth for comparison. The $watchCollection type will register a change event when any of the object's properties are modified, but it is unconcerned with what those properties refer to.

How to do it…

This watcher is best used with arrays or flat objects that undergo frequent top-level property modifications or reassignments. Currently, it does not provide the modified property(s) responsible for the callback, only the entire objects, so the callback is responsible for determining which properties or indices are incongruent. This can be done as follows:

$scope.myObj = {
  myPrimitive: 'Go Bears!',
  myArray: [3,1,4,1,5,9]
};

// watch myObj and all top-level properties by reference
$scope.$watchCollection('myObj', function(newVal, oldVal, scope) {
  // callback logic
});

// watch myObj.myArr and all its elements by reference
$scope...