$watch Simple to use
$watch is a scope function that listens for model changes and notifies you when the part of your model changes.
$watch (watchexpression, Listener, objectequality);
The description of each parameter is as follows:
Watchexpression: The Listener object, which can be a angular expression such as ' name ', or function such as functions () {return $scope. Name}.
Listener: The function or expression that will be called when watchexpression changes, it receives 3 parameters: NewValue (new value), OldValue (old value), scope (scope reference);
Objectequality is a Boolean parameter, and if set to True, this optional boolean parameter will command angular to check whether each property of the monitored object has changed. You can use this parameter if you want to monitor an element in an array, or all of the properties on an object, rather than just monitoring a simple value. Because angular need to traverse arrays or objects, if the collection is larger, the computational burden will be heavier.
$watch function returns a function that, when you no longer need to receive change notifications, can unregister the monitor with the returned function.
If we need to monitor a property and then unregister the monitor, we can use the following code:
...
var dereg = $scope. $watch (' Somemodel.someproperty ', Callbackonchange ());
...
Dereg ();
[email protected]$scope. Watch Monitor model changes