<!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