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

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

问题描述

我对我的 AngularJS 应用有了一个想法,我很好奇 AngularJS 社区是否会认为这样做可以.简而言之,我正在连接到数据 API 并在页面上显示我的结果.

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. In short, I am connecting to a data API and displaying my results on the page.

我创建了一个 AngularJS 服务,它在 $rootScope.DataStore 上创建了一个数据存储.我还有一个服务方法,它使用从 API 端点返回的数据更新 DataStore.如果我使用 DataStore.update('products') 从我的控制器内部请求产品"API 端点,这将使用我的产品数据更新 $rootScope.DataStore.products.

I have created an AngularJS 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.

现在,在视图/部分中,我需要做的就是说 ng-repeat="product in DataStore.products" 来显示我的数据,并且控制器范围无关紧要我在.所以,从本质上讲,我的 DataStore 是我唯一的事实来源.

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 更新,任何绑定到 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 中有解决此处引用的常见问题解答:

有时您希望将一些数据全局化整个应用程序.对于这些,您可以注入 $rootScope 并设置值它就像任何其他范围.由于作用域继承自根作用域,这些值将可用于附加到的表达式像 ng-show 这样的指令就像你本地 $scope 上的值一样.

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,就像您(希望)在任何语言中使用全局变量一样.特别是,不要将它用于代码,仅用于数据.如果你想把一个函数放在 $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.

这不会给 $digest 循环(它实现基本的脏检查以测试数据突变)带来太多负载,而且这不是一种奇怪的做事方式.

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 开发人员)在 SO 上的答案:数据绑定在 AngularJS 中是如何工作的? 特别注意性能部分.

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

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

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