javascript - Difference between $rootScope and $rootScope.$root -


is there difference between $rootscope , $rootscope.$root?

what difference between

$rootscope.global.flag = true , $rootscope.$root.global.flag = true

do both of them access same variable in rootscope?

if so, there particular situation have use either of them?

all scopes in angular instances of same prototype. such, global service $rootscope same type of object created directives , passed link function $scope, or controllers.

the property $root part of prototype , available on scopes.

the $rootscope first scope created angular. scopes created using $new method existing scope. $rootscope special case because it's created before angular.run() executed on modules.

when check value of $scope.$root references same instance provided root scope service $rootscope.

therefore;

console.log($rootscope === $scope.$root); // print true 

or in example;

console.log($rootscope === $rootscope.$root); // print true 

so yes, variables in root scope same no matter how reference root scope.

console.log($rootscope.global.flag); // prints true console.log($scope.$root.global.flag); // prints true console.log($rootscope.$root.global.flag); // prints true 

you can explicitly access root scope in template expressions this.

<div>{{$root.somevalue}}</div> 

there other properties $parent let walk chain of scopes, $parent null isolated scopes (since has no parent).


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -