$scope的区别是什么?根和rootScope美元吗?

时间:2021-03-27 13:32:37

I see in controllers that $scope has $root, what is this? How is it different from $rootScope which can be injected in the controller?

我在控制器中看到$scope有$root,这是什么?它与可以注入控制器的$rootScope有何不同?

2 个解决方案

#1


62  

$rootScope var which points to the parent of all the scopes and can be injected everywhere. All other scopes are children of the $rootScope. They are created via the $new method of the $rootScope thus every scope inherits from the $rootScope.

$rootScope var指向所有作用域的父对象,可以在任何地方进行注入。所有其他的作用域都是$rootScope的子作用域。它们是通过$rootScope的$new方法创建的,因此每个范围都继承自$rootScope。

In the angular source in the definition of the Scope constructor there is a line :

在角源定义的作用域构造函数中有一条线:

 function Scope() {
   this.$id = nextUid();
 ...
 this['this'] = this.$root =  this;
 ...

It seems the $root var is just a placeholder for this of the first scope created - $rootScope.

看起来$root var只是第一个创建范围的占位符—$rootScope。

Next there is this piece of code in the $new method:

下面是$new方法中的这段代码:

  $new: function(isolate) {
      ...

    if (isolate) {
      child = new Scope();
      child.$root = this.$root;
   ...
   return child;

So the $root var of every scope child of $rootScope is a reference to $rootScope. And all the children of those children will get the same reference to $rootScope

所以$rootScope的每个作用域子代的$root var是对$rootScope的引用。这些孩子的所有孩子都会得到$rootScope的相同引用

In my opinion it is better to use the $rootScope via dependency injection because it is an explicit and overall more frequently used way of referring to the $rootScope

在我看来,最好通过依赖注入使用$rootScope,因为它是一种更明确和更常用的引用$rootScope的方式

#2


25  

As mentioned before, $scope.$root holds a reference to the $rootScope.

如前所述,美元范围。$root持有对$rootScope的引用。

Unfortunately, there IS a difference between using $scope.$root and using $rootScope:

不幸的是,使用$scope之间存在差异。根和使用rootScope美元:

  1. When $scope IS the root, its $root property is null
  2. 当$scope是根时,它的$root属性为null
  3. $scope.$root is only assigned on isolate scopes: https://github.com/angular/angular.js/blob/v1.3.6/src/ng/rootScope.js#L204
  4. 美元的范围。$root仅在隔离作用域中分配:https://github.com/angular/angular.js/blob/v1.3.6/src/ng/rootScope.js#L204

So you might have a situation where $scope.$root is null. Better use $rootScope instead...

你可能会遇到$scope。美元的根是null。更好地利用rootScope美元而不是……

#1


62  

$rootScope var which points to the parent of all the scopes and can be injected everywhere. All other scopes are children of the $rootScope. They are created via the $new method of the $rootScope thus every scope inherits from the $rootScope.

$rootScope var指向所有作用域的父对象,可以在任何地方进行注入。所有其他的作用域都是$rootScope的子作用域。它们是通过$rootScope的$new方法创建的,因此每个范围都继承自$rootScope。

In the angular source in the definition of the Scope constructor there is a line :

在角源定义的作用域构造函数中有一条线:

 function Scope() {
   this.$id = nextUid();
 ...
 this['this'] = this.$root =  this;
 ...

It seems the $root var is just a placeholder for this of the first scope created - $rootScope.

看起来$root var只是第一个创建范围的占位符—$rootScope。

Next there is this piece of code in the $new method:

下面是$new方法中的这段代码:

  $new: function(isolate) {
      ...

    if (isolate) {
      child = new Scope();
      child.$root = this.$root;
   ...
   return child;

So the $root var of every scope child of $rootScope is a reference to $rootScope. And all the children of those children will get the same reference to $rootScope

所以$rootScope的每个作用域子代的$root var是对$rootScope的引用。这些孩子的所有孩子都会得到$rootScope的相同引用

In my opinion it is better to use the $rootScope via dependency injection because it is an explicit and overall more frequently used way of referring to the $rootScope

在我看来,最好通过依赖注入使用$rootScope,因为它是一种更明确和更常用的引用$rootScope的方式

#2


25  

As mentioned before, $scope.$root holds a reference to the $rootScope.

如前所述,美元范围。$root持有对$rootScope的引用。

Unfortunately, there IS a difference between using $scope.$root and using $rootScope:

不幸的是,使用$scope之间存在差异。根和使用rootScope美元:

  1. When $scope IS the root, its $root property is null
  2. 当$scope是根时,它的$root属性为null
  3. $scope.$root is only assigned on isolate scopes: https://github.com/angular/angular.js/blob/v1.3.6/src/ng/rootScope.js#L204
  4. 美元的范围。$root仅在隔离作用域中分配:https://github.com/angular/angular.js/blob/v1.3.6/src/ng/rootScope.js#L204

So you might have a situation where $scope.$root is null. Better use $rootScope instead...

你可能会遇到$scope。美元的根是null。更好地利用rootScope美元而不是……