Brief introduction
First, the Apply method will trigger the Evel method, and when the Evel method resolves successfully, it will trigger the digest method, and the digest method will trigger the watch method.
When Digest executes, it is triggered if watch observes that the value is not the same as the last execution.
Angularjs's internal watch realizes the timely updating of the page with the model.
$watch method is used primarily to manually listen for an object, but an event is triggered when the object changes. 】
Grammar
$watch (Watchfn,watchaction,deepwatch)
WATCHFN: This parameter is a string with an angular expression or function that returns the current value of the data model being monitored. This expression will be executed many times, so make sure it doesn't produce any other side effects. In other words, make sure it can be called many times without changing the state. For the same reason, the monitoring expression should be easily computed. If you use a string to pass a angular expression, it will be executed against the object in the scope that called it.
watchaction (newvalue,oldvalue,scope): This is a function or expression that will be called when the WATCHFN is changed. If it is in the form of a function, it will receive the new and old two values of WATCHFN, as well as a reference to the scope object. Its function signature is functions (NewValue, OldValue, scope).
Deepwatch: 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 will
return a function that you want to unregister this watch with.
var dereg = $scope. $watch (' Somemodel.someproperty ', Callbackonchange ()); Dereg ();
Usage
Effect: Http://runjs.cn/detail/vnlh0eny
Angularjs Watch