AngularJS watch

$scope.watch()

function(watchExp, listener, objectEquality, prettyPrintExpression)
  • watchExp: The expression being watched. It can be a function or a string,it is evaluated at every digest cycle.
  • listener: A callback, fired when the watch is first set, and then each time that during the digest cycle that a change for watchExp’s value is detected.
  • objectEquality: true for deep comparison, false for shallow comparison.
  • prettyPrintExpression: used internally by expression parser.

$scope.$watchGroup()

Allows to set up many watchers with the same callback, passing an array of watchExpressions.
$scope.$watchGroup(['obj.a', 'obj.b.bb[1]', 'letters[2]'], 
                   function(newValues, oldValues, scope) {
//...
});
newValues and oldValues will hold a list of values for the expressions, in the same order as they are passed in the first parameter’s array.
Doesn’t allow objectEquality, it only shallow watches the expressions, and only reacts to reference changes.

$scope.$watchCollection()

Convenient shortcut to watch arrays or objects.
  • Arrays: the listener will be called when any of the elements is replaced, deleted, or added.
  • Objects: the listener will be called when any first-level property is changed.

Doesn’t allow objectEquality, it only shallow watches elements/fields, and won’t react on changes to their subfields.

Refer from https://www.sitepoint.com/mastering-watch-angularjs/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑