Angularjs to realize the change of multiple values on a single monitor _angularjs

Source: Internet
Author: User


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.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.