Scopes and controllers

Source: Internet
Author: User

ANGULARJS Notes---Scopes and controllers

What is a scope. What is a controller, a scope that contains the functionality and data required to render a view, which is the only source of all views. You can interpret the scope as an attempt model (ViewModel). The scope can be either an inclusive relationship or an independent relationship. You can set the $scope to be under different scopes by setting different ng-controller.

I. $rootScope

1.1ROOTSCOPECanToActingSolutionYesAllCouncilChangeAmount,OneDenierFuValueRight whole mod< Span id= "mathjax-span-64" class= "Mi" >ul e all there effect Rootscpoe does not depend on any controller, App.run is the code that is executed when the module is loaded. Can be understood as the OnInit method inside Silverlight MVVM.

1.2 Angularjs takes precedence over the value added by $scope in the controller.

The code is as follows: There are three {message} respectively. The message value is re-assigned in the TestControl2 in the $scope.

Run Result: The message under TestControl2 has been modified.

two. Controller (Ng-controller)

Ng-controller atmosphere Two situations one is to contain, the other is to be independent. Let's call it a father-son relationship and a brotherhood.

2.1 Parent-child relationship

The 2.1.1 code is as follows: Childcontroller is included in the Parentcontroller, and two buttons are assigned to the Count field.

//$scope. Count = 1;$scope. AddChild = function () {$scope.         Count+=1;    }    }); </script>

2.1.2 Run Result: (in the demo above, there is a string of commented out code,)

A code is annotated: when the first click Parentclick Two count together, and then click Childclick when only Childdiv count is added, and then go back to click Parentclick, Only Parentcount in the self-increasing

B. The code is not annotated: the self-increment between the two is separate, complementary and interrelated.

2.1.3 Summary:

Because in the ANGULARJS will default to find the current controller under the value, find no more to find the parent controller value, if you can not find it to findROOTSCOPEOfValue,RightInAClassCiaConditionInDidn'tPointClickCHILDCLICKOfWhenHou,CHILDCONTROLLERAlsoDidn'tYesSetResetCOUNTOfValue,That?RightShouldOfCOUNTOnlyYesFirstToTakePARENtc on tr ol le r The value of the Scope.count. When Childclick is clicked, the $scope.count in Childcontroller is created, and then two controllers start to stand alone.

2.2 Brotherly relations.

Running Result: Count independent under two controllers, self-increment does not affect each other.

 

Three. Summary of this article

A. $rootScope = = global variable B. The bound variable is first taken from the current controller's $scope by default

Tags: Angularjs learning Notes

Scopes and controllers

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.