now I want to display a variable on the page i,i the initial value is 0, I want to pass the controller every 1 seconds to I plus 1, make on the page I from 0,1,2,3,4 ... has been growing and I will do this:
<script type= "Text/javascript" > /** * module * * description */ angular.module (' app ', []). Controller (' Firstcontroller ', function ($scope) { $scope .i = 0; setinterval (function () { $scope .i++; }, 1000); }); </script>
<script type= "Text/javascript" > /** * module * * description */ angular.module (' app ', []). Controller (' Firstcontroller ', function ($scope) { $scope .i = 0; setinterval (function () { $scope. $apply (function () { $scope .i++; }); }, 1000); });</script>
Angular knows that variable changes are implemented through dirty checking, which is to copy a snapshot of the original object, and at some time compare the values of the object and the snapshot, if not the same. Angular does not dirty check all objects, and when the object is bound to HTML, the object is added as a check object. Also does not check all properties and will be listed as checked properties when the property is bound.
All events that begin with NG will trigger a dirty check when they are executed. And here, in its own firstcontroller, there is obviously no dirty check triggered after each loop execution function ends. $apply method is to manually trigger the dirty check, $apply just go into angular context and then trigger the dirty check by $digest. The $digest method is responsible for the dirty checks of the scope and child scopes that belong to, and the dirty check triggers the $watch method.
$apply () is defined as:
$apply: function (expr) { try { beginphase (' $apply '); return this. $eval (expr); } catch (e) { $exceptionHandler (e); } finally { clearphase (); try { $rootScope. $digest (); } catch (e) { $exceptionHandler (e); throw e; } } }
When $digest executes, the $watch method is triggered if watch observes that the value is not the same as the last one.
$watch (Watchexp,watchaction,deepwatch)
Watchexp indicate the object to listen to, if watchexp change, will execute watchaction,deepwatch to give an example explanation.
<! doctype html>when Watchexp is an array, Deepwatch is set to true, as long as the name or age in ABC is changed, the arithmetic group ABC changes, if the Deepwatch is set to False, change the name or age, ABC has not changed and will not execute watchacion. The above code is as long as name or age change, I will increment by 1.
AngularJS Study Notes (iii)