$ rootScope和$ rootScope之间的区别。$ root

时间:2021-01-09 13:32:11

Is there any difference between $rootScope and $rootScope.$root?

$ rootScope和$ rootScope之间有什么区别。$ root?

What is the difference between

有什么区别

$rootScope.global.flag = true and $rootScope.$root.global.flag = true

$ rootScope.global.flag = true和$ rootScope。$ root.global.flag = true

Do both of them access the same variable in rootscope?

他们俩都是在rootscope中访问同一个变量吗?

If so, is there any particular situation where we have to use either of them?

如果是这样,是否有任何特殊情况我们必须使用它们中的任何一个?

1 个解决方案

#1


3  

All scopes in Angular are instances of the same prototype. As such, the global service $rootScope is the same type of object created for directives and passed to the link function as $scope, or for controllers.

Angular中的所有范围都是相同原型的实例。因此,全局服务$ rootScope是为指令创建的相同类型的对象,并作为$ scope或控制器传递给链接函数。

The property $root is part of that prototype and available on all scopes.

属性$ root是该原型的一部分,可在所有范围内使用。

The $rootScope is the first scope created by Angular. All scopes are created by using the $new method from an existing scope. So $rootScope is a special case because it's created before angular.run() is executed on modules.

$ rootScope是Angular创建的第一个范围。所有范围都是使用现有范围中的$ new方法创建的。所以$ rootScope是一个特例,因为它是在模块上执行angular.run()之前创建的。

When you check the value of $scope.$root it references the same instance that is provided by the root scope service for $rootScope.

当您检查$ scope。$ root的值时,它引用$ rootScope的根作用域服务提供的同一实例。

Therefore;

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

Or as in your example;

或者如你的例子;

console.log($rootScope === $rootScope.$root); // will also print true

So yes, the variables in the root scope are the same no matter how you reference the 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 also explicitly access the root scope in template expressions like this.

您还可以显式访问模板表达式中的根范围,如下所示。

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

There are other properties like $parent that let you walk up the chain of scopes, but $parent will be null for isolated scopes (since it has no parent).

还有其他属性,比如$ parent,可以让你走向范围链,但是对于孤立的范围,$ parent将为null(因为它没有父级)。

#1


3  

All scopes in Angular are instances of the same prototype. As such, the global service $rootScope is the same type of object created for directives and passed to the link function as $scope, or for controllers.

Angular中的所有范围都是相同原型的实例。因此,全局服务$ rootScope是为指令创建的相同类型的对象,并作为$ scope或控制器传递给链接函数。

The property $root is part of that prototype and available on all scopes.

属性$ root是该原型的一部分,可在所有范围内使用。

The $rootScope is the first scope created by Angular. All scopes are created by using the $new method from an existing scope. So $rootScope is a special case because it's created before angular.run() is executed on modules.

$ rootScope是Angular创建的第一个范围。所有范围都是使用现有范围中的$ new方法创建的。所以$ rootScope是一个特例,因为它是在模块上执行angular.run()之前创建的。

When you check the value of $scope.$root it references the same instance that is provided by the root scope service for $rootScope.

当您检查$ scope。$ root的值时,它引用$ rootScope的根作用域服务提供的同一实例。

Therefore;

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

Or as in your example;

或者如你的例子;

console.log($rootScope === $rootScope.$root); // will also print true

So yes, the variables in the root scope are the same no matter how you reference the 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 also explicitly access the root scope in template expressions like this.

您还可以显式访问模板表达式中的根范围,如下所示。

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

There are other properties like $parent that let you walk up the chain of scopes, but $parent will be null for isolated scopes (since it has no parent).

还有其他属性,比如$ parent,可以让你走向范围链,但是对于孤立的范围,$ parent将为null(因为它没有父级)。