4th Chapter Scope

Source: Internet
Author: User

the world of views and $scope

  Angularjs starts and generates a view, binding Ng-app with $rootscope. $rootScope is the top level of all $scope objects. Corresponds to the global scope of a ng-app. $scope object acts only as a data model, without processing and manipulating data.

The following is an example of $rootscope:

  

<!DOCTYPE HTML><HTML>    <Head>        <Scriptsrc= "Angularjs/1.2.13/angular.js"></Script>    </Head>    <BodyNg-app= "MYAPP">        <Div>Hello {{name}}</Div>    </Body>    <Script>        varapp=Angular.module ('myApp', []). Run (function($rootScope) {$rootScope. Name=" World";    }); </Script></HTML>

  The above example sets a name variable in $rootscope and references it. To avoid polluting the global namespace, you can explicitly create an isolated $scope object with the controller, and then set the variable to this sub-variable. You can attach a controller object to a DOM element by using the Ng-controller directive. As follows:

  

<!DOCTYPE HTML><HTML>    <Head>        <Scriptsrc= "Angularjs/1.2.13/angular.js"></Script>    </Head>    <BodyNg-app= "MYAPP">        <DivNg-controller= "Mycontroller">Hello {{name}}</Div>    </Body>    <Script>        varapp=Angular.module ('myApp', []); App.controller ("Mycontroller",function($scope) {$scope. Name="Ari"; })    </Script></HTML>
the life cycle of the $scope

  1. Create

When you create a controller or instruction, $injector create a new scope and pass the scope in when the new controller or instruction runs.

2. Links

When angular starts running, all $scope objects are attached to the view.

3. Update

When an event loop runs, the top-level $scope object is typically executed, and each child scope performs its own dirty-value detection. When a transform is detected, the specified callback function is triggered.

4. Destruction

directives and Scopes

Directives typically do not create their own scopes, but the Ng-controller and ng-repeat directives create their own scopes.

4th Chapter Scope

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.