AngularJS使用$ rootScope作为数据存储 [英] AngularJS using $rootScope as a data store

查看:135
本文介绍了AngularJS使用$ rootScope作为数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的AngularJS应用程序的想法,我很好奇,如果AngularJS社会将认为这是好做这样...

I have an idea for my AngularJS app and I'm curious if the AngularJS community would consider it okay to do it this way...

总之,我连接到​​数据API,并在页面上展示我的结果。我已经创建,创建于$ rootScope.DataStore数据存储的角度服务。我也有与更新从API端点返回的数据存储中的服务方法。如果我从我的DataStore.update控制器(产品)的内部要求产品API端点,这将更新$用我的产品数据rootScope.DataStore.products。现在,在查看/分,所有我需要做的是说NG-重复=产品在DataStore.products,以示我的数据,不要紧,我有什么控制的范围。所以,从本质上我的DataStore是我的真心话的单一来源。

In short, I am connecting to a data API and displaying my results on the page. I have created an angular service that creates a data store on $rootScope.DataStore. I also have a service method that updates the DataStore with the data returned from an API endpoint. If I request the "products" API endpoint from inside my controller with DataStore.update('products'), this would update $rootScope.DataStore.products with my product data. Now, in the view/partial, all I need to do is say ng-repeat="product in DataStore.products" to show my data, and it doesn't matter what controller scope I am in. So, in essence my DataStore is my single source of truth.

我感觉是什么样的,我从这个方法获得很容易遵循的语义和最小的控制器编码。因此,任何时候,数据存储是更新的,这是必然的DataStore什么也得到更新。

What I feel like I gain from this method is easy to follow semantics and minimal controller coding. So, anytime the DataStore is updated, anything that's bound to DataStore also gets updated.

请问这把太多的负载上的$ rootScope消化周期,或者这只是一种奇怪的方式做到这一点?或者是一个完全真棒呀? :)任何意见都欢迎。

Would this put too much load on the $rootScope digest cycle, or is this just an odd way to do it? Or is it a totally awesome way? :) Any comments are welcome.

推荐答案

这个问题是在 AngularJS常见问题解决报价这里:

This question is addressed in the AngularJS FAQ quoted here:

偶尔有一些你想使全局数据块
  整个应用程序。对于这些,您可以在注入$ rootScope 并设定值
  它像任何其他范围。由于示波器从根范围继承,
  这些值将提供给附连到前pressions
  像吴秀指令就像值在本地$范围。

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.

看来,球队并鼓励使用 $ rootScope 这样一来,这个警告:

It seems that the team does encourage using $rootScope this way, with this caveat:

当然,全局状态吸你应该谨慎使用$ rootScope,
  就像你(希望)用任何语言全局变量使用。
  特别是,不要使用它code,唯一的数据。如果你动心
  穿上$ rootScope功能,它几乎总是最好把它放在一个
  服务在需要的地方,并且更容易,可以注射
  测试。

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.

这并没有把太多的负载上的 $消化周期(实现基本脏检查,测试数据突变),这是不是一种奇怪的方式做事

This does not put too much load on the $digest cycle (which implements basic dirty checking to test for data mutation) and this is not an odd way to do things.

编辑:有关性能的更多详细信息,请参阅从MISKO这个答案(AngularJS DEV)在这里这样:<一href=\"http://stackoverflow.com/questions/9682092/angularjs-how-does-databinding-work/9693933#9693933\">AngularJS :?如何绑定工作特别注意对性能的部分。

For more details on performance, see this answer from Misko (AngularJS dev) here on SO: AngularJS : How does databinding work? Specifically note the section on performance.

这篇关于AngularJS使用$ rootScope作为数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆