当工厂可以做同样的事情时,为什么要使用 $rootScope 在控制器之间共享数据?
Why use $rootScope to share data across controllers when factories can do the same thing?
我的理解是 $rootScope 是某种全局变量,允许它在控制器之间共享。工厂也可以做同样的事情。那么,为什么不使用工厂呢?什么时候应该使用 $rootScope,什么时候应该使用工厂,因为它们的用途几乎相同?
提供 $rootScope 以查看应用程序中范围的层次结构。如果您有直接修改层次结构的任务(这种情况很少见:例如手动修复由 3rd 方库或某些全局事件发射器引起的内存泄漏),则使用 $rootScope.
在所有其他 99% 的情况下,应该使用服务。
另见意见here.
如果你想存储一些数据并在路由更改后保留它 - 服务(工厂)是最好的解决方案。 $rootScope
可以做同样的事情,但它是全球性的,正如你提到的,所以有机会破坏你的数据。 $rootScope
在需要一些全局事件 ($rootScope.$broadcast
) 时很有用,例如用户 login/logout,但不适用于数据存储。
AngularJS FAQ 已经很好地回答了这个问题 here:
$rootScope exists, but it can be used for evil
Scopes in Angular form
a hierarchy, prototypally inheriting from a root scope at the top of
the tree. Usually this can be ignored, since most views have a
controller, and therefore a scope, of their own.
Occasionally there are pieces of data that you want to make global to
the whole app. For these, you can inject $rootScope and set values on
it like any other scope. Since the scopes inherit from the root scope,
these values will be available to the expressions attached to
directives like ng-show just like values on your local $scope.
Of course, global state sucks and you should use $rootScope sparingly,
like you would (hopefully) use with global variables in any language.
In particular, don't use it for code, only data. If you're tempted to
put a function on $rootScope, it's almost always better to put it in a
service that can be injected where it's needed, and more easily
tested.
Conversely, don't create a service whose only purpose in life is to
store and return bits of data.
我的理解是 $rootScope 是某种全局变量,允许它在控制器之间共享。工厂也可以做同样的事情。那么,为什么不使用工厂呢?什么时候应该使用 $rootScope,什么时候应该使用工厂,因为它们的用途几乎相同?
提供 $rootScope 以查看应用程序中范围的层次结构。如果您有直接修改层次结构的任务(这种情况很少见:例如手动修复由 3rd 方库或某些全局事件发射器引起的内存泄漏),则使用 $rootScope.
在所有其他 99% 的情况下,应该使用服务。
另见意见here.
如果你想存储一些数据并在路由更改后保留它 - 服务(工厂)是最好的解决方案。 $rootScope
可以做同样的事情,但它是全球性的,正如你提到的,所以有机会破坏你的数据。 $rootScope
在需要一些全局事件 ($rootScope.$broadcast
) 时很有用,例如用户 login/logout,但不适用于数据存储。
AngularJS FAQ 已经很好地回答了这个问题 here:
$rootScope exists, but it can be used for evil
Scopes in Angular form a hierarchy, prototypally inheriting from a root scope at the top of the tree. Usually this can be ignored, since most views have a controller, and therefore a scope, of their own.
Occasionally there are pieces of data that you want to make global to the whole app. For these, you can inject $rootScope and set values on it like any other scope. Since the scopes inherit from the root scope, these values will be available to the expressions attached to directives like ng-show just like values on your local $scope.
Of course, global state sucks and you should use $rootScope sparingly, like you would (hopefully) use with global variables in any language. In particular, don't use it for code, only data. If you're tempted to put a function on $rootScope, it's almost always better to put it in a service that can be injected where it's needed, and more easily tested.
Conversely, don't create a service whose only purpose in life is to store and return bits of data.