Angularjs--watch () method

Source: Internet
Author: User

One of the things we often need to do when writing an application with ANGULAJS is to monitor the variables in the model and respond accordingly to the changes that occur. ANGULARJS provides us with a very handy $watch method that helps us to monitor the variables in each scope. The following is a very simple example:

  


<!
DOCTYPE HTML><HTMLNg-app><Head> <MetaCharSet= "Utf-8"> <title>2, Angularjs's $watch () method</title> <Scriptsrc=".. /js/angularjs.js "></Script> <Scriptsrc=".. /js/index7.js "></Script></Head><Body><!--ANGULARJS Data binding, two-way binding, binding between M,v,c - <DivNg-controller= "Firstcontroller"> <inputtype= "text"Ng-model= "Name"value="" /> <P>Number of changes: {{count}}---{{name}}</P> </Div></Body></HTML>
var firstcontroller=function($scope) {     $scope. Name= "Zhang San";     $scope. Count=0;    $scope. $watch ("name",function(newvalue,oldvalue) {        console.log (newvalue,oldvalue);         + +$scope. Count        ; if ($scope. count>10) {            $scope. Name= "Input has changed 10 times";};}    );}

Inside Angularjs, every time we make a modification to the Name property of the Ng-model binding, Angularjs internal $digest runs once and checks what we use $watch to monitor after the run is done, and if and when the last $ Digest before the change, execute the handler function in which we bind.

Angularjs--watch () method

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.