Angularjs's $watch and $apply

Source: Internet
Author: User

<!DOCTYPE HTML><HTML><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title></title></Head><BodyNg-app= "Fng">    <DivNg-controller= "FC1">        <DivNg-bind= "TimeNow"></Div>        <DivNg-bind= "TimeNow2"></Div>        <HR>        <DivNg-bind= "Test.count"></Div>        <DivNg-bind= "Test.name"></Div>        <inputtype= "button"value= "Add"Ng-click= "Add ()"/>    </Div></Body><Scriptsrc= "Angular.js"></Script><Script>    varNgapp=Angular.module ("Fng",[]); Ngapp.controller ("FC1",['$scope','$http',function($scope) {$scope. TimeNow=NewDate (); SetInterval (function(){            //from a macroscopic point of view $scope. $apply is much larger than $scope $watch.            /** $scope. The $scope local member to be updated is given inside the $apply method, so that the local update listener for NG is triggered (if $digest is used then the entire $scope The changes in the members will be monitored, the efficiency is greatly reduced) **/$scope. $apply (function() {                if (true) {$scope. TimeNow= NewDate (); $scope. TimeNow2= NewDate ();        }            });        }); //=====================================================$scope. Test={count:0, Name:"less than ten"        }; $scope. Add=function() {$scope. Test.count+=1;        }; //This is designed for you to track a member of your own        /** $scope. The $watch provides three parameters (object member/other member, callback function, (optional) * If the property of the object member * is changed) * The third argument is not filled, only the object becomes an array or other type to trigger the callback function * */$scope. $watch ('Test',function(){            if($scope. Test.count>Ten) {$scope. Test.name="More than 10! "; }        },true); }]);</Script></HTML>

Angularjs's $watch and $apply

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.