简体   繁体   English

为什么要使用$ rootScope?

[英]Why would you ever use the $rootScope?

Ok so I was reading here 好,所以我在这里读书

basically when I have this 基本上当我有这个

MyApp = MyApp || {};

MyApp.settings = {
    isFooEnabled: false
}

if I use the rootscope and want to check if isFooEnabled I have to inject the rootScope into whatever object I want to do the check. 如果我使用rootscope并要检查isFooEnabled ,则必须将rootScope注入要执行检查的任何对象中。

How does that make sense? 这有什么意义?

What is the advantage of using $rootScope.isFooEnabled over using straight standard javascript MyApp.isFooEnabled ? 与使用直接的标准javascript MyApp.isFooEnabled相比,使用$rootScope.isFooEnabled有什么优势?

what is better for what? 有什么好处呢?

when should I use one over the other? 我什么时候应该使用另一个?

The $rootScope is the top-most scope. $ rootScope是最高级的范围。 An app can have only one $rootScope which will be shared among all the components of an app. 一个应用程序只能有一个$ rootScope,它将在该应用程序的所有组件之间共享。 Hence it acts like a global variable. 因此,它就像一个全局变量。 All other $scopes are children of the $rootScope. 所有其他$ scope是$ rootScope的子级。

The rootScope's variable is set when the module initializes, and then each of the inherited scope's get their own copy which can be set independently. 在模块初始化时,将设置rootScope的变量,然后每个继承的作用域将获得自己的副本,该副本可以独立设置。

NOTE: 注意:

  1. When you use ng-model with $rootScope objects then AngularJS updates those objects under a specific $scope of a controller but not at global level $rootScope. 当您将ng-model与$ rootScope对象一起使用时,AngularJS将在控制器的特定$ scope下而不是在全局级别$ rootScope下更新这些对象。

  2. The $rootScope shouldn't be used to share variables when we have things like services and factories. 当我们拥有服务和工厂之类的东西时,不应使用$ rootScope共享变量。

Finally, Angular FAQ says this at the bottom of the page: "Conversely, don't create a service whose only purpose in life is to store and return bits of data." 最后,Angular FAQ在页面底部这样说:“相反,不要创建其唯一的目的是存储和返回数据的服务。” See from here . 这里看

Actually, I would argue that you shouldn't use $rootScope in this case, you should create a separate service (or factory) that stores your settings, however, the usage and reasons are the same. 实际上,我认为在这种情况下您不应该使用$rootScope ,应该创建一个单独的服务(或工厂)来存储您的设置,但是用法和原因是相同的。

For simply storing values, the primary reason is consistency. 为了简单地存储值,主要原因是一致性。 Modules and dependency injection are a big part of angular to ensure you write testable code, and these all use dependency injection so that unit tests can be written easily (dependencies can be mocked). 模块和依赖注入是确保您编写可测试代码的重要组成部分,并且所有这些都使用依赖注入,因此可以轻松编写单元测试(可以模拟依赖)。 Whilst there are not many obvious gains from injecting a simple object, it's consistent with the way more complex code is accessed, and there is a lot to be said for that. 尽管注入一个简单的对象并不会带来很多明显的好处,但这与访问更复杂的代码的方式是一致的,对此有很多话要说。 On a similar note, if you were to upgrade your settings object to fetch data from the server (eg for environment specific settings), you might want to start unit testing that functionality, which you can't really do properly without modularising it. 与此类似,如果要升级设置对象以从服务器获取数据(例如,针对特定环境的设置),则可能要开始对该功能进行单元测试,而如果不对它进行模块化,则无法真正完成该功能。

There is also the (frankly weak) namespacing argument - what if another library you import uses window.MyApp ? 还有一个(坦白地说很弱)的命名空间参数-如果导入的另一个库使用window.MyApp怎么办?

TL;DR: It's a strongly recommended best-practice. TL; DR:强烈建议采用最佳做法。 It might seem a bit clunky now, but you'll benefit from doing it in the long run. 现在看来似乎有些笨拙,但从长远来看,您会从中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM