The difference between scope and rootscope in Angularjs

Source: Internet
Author: User


1,js Code


The code is as follows

Phonecatapp.controller (' Testctrl ', [' $scope ', ' $rootScope ',
function ($scope, $rootScope) {
$rootScope. Name = ' This is test ';
}
]);

Phonecatapp.controller (' Test111ctrl ', [' $scope ', ' $rootScope ',
function ($scope, $rootScope) {
$scope. Name = $rootScope. Name;
}
]);


2,html Code


The code is as follows

<div ng-controller= "Testctrl" >
Iset the global variable.<strong>{{$root .name}}</strong>
</div>

<div ng-controller= "Test111ctrl" >
1,get global variable .<strong>{{name}}</strong><br>
2,get global variable. <strong>{{$root .name}}</strong>
</div>www.111cn.net


3, Show results


The code is as follows

I set the global variable.this is test
1,get global variable. This is test
2,get global variable. This is test


As you can see from the results, $rootScope. Name set variables, in all controller can be directly with the {{$root. Name}} to display, very powerful. That, of course, can also be assigned a value to scope.


Related Article

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.