AngularJS Study Notes (iii)

Source: Internet
Author: User

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)

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.