First, $watch simple use
$watchis ascopefunction that listens to the changes in the model and notifies you when your model is partially changed.
$watch (watchexpression, Listener, objectequality);
Each parameter is described as follows:
watchexpression: A Listener object that can be a angular expression such as ' name ', or a function such asfunction(){return $scope.name}.
Listener: awatchExpressionfunction or expression that is invoked when it changes, receiving 3 parameters: (new value), (newValueoldValueold value),scope(reference to the scope)
objectequality: depth listening, if set to true, tells angular to check for changes to each property in the object being monitored. If you want to monitor the individual elements of an array or the properties of an object rather than an ordinary value, you should use it
Give me a chestnut:
$scope. Name = ' Hello ';
var watch = $scope. $watch (' name ', function (Newvalue,oldvalue, scope) {
console.log (newvalue);
Console.log (OldValue);
});
$timeout (function () {
$scope. Name = "World";
},1000);
Second, monitor the change of multiple values
What you usually encounter is$watch()a change in the value of a single listener, which, of course, satisfies most situations at this time. But by reading the official website for$watch()The explanation,$watch()there is also a third parameter, the third parameter is a Boolean type, which indicates whether the depth of the monitoring, the depth of the example is the object of comparison of objects properties.
This allows us to implement a change that listens for multiple values at a time.
Sample code
var app=angular.module ("Watchapp", [])
. Controller ("Watchcontroller", ["$scope", function ($scope) {
$ scope.object={};
$scope. object.one= $scope. One;
$scope. object.one= $scope. One;
$scope. $watch ("Object", function () {
...
},true);
}]
Summarize
The above is about how ANGULARJS to monitor multiple values change all the content, we have learned it? Hope that the content of this article for everyone's study and work can help, if there is doubt can message exchange, thank you for the cloud Habitat Community support.