使用$ rootScope的引用存储服务 [英] Use $rootScope to store a reference to a service

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

问题描述

我的工作中/大尺寸的应用,我们有存储在服务当前用户的数据和方法。因为我们需要在整个应用程序的用户信息,这将是一个坏主意,附上这个服务于 $ rootScope 一次?

I'm working on a medium / big size application where we have the current user data and methods stored on a Service. Because we need the user info across the application, would it be a bad idea to attach this service to the $rootScope once?

AppController的

$rootScope.user = user.get();

而不是增加它放在一个Controller基(这将是在控制器的95%):

Instead of adding it on a Controller base (which would be in the 95% of the Controllers):

$scope.user = user.get();

此外,是否有任何内存/性能问题?

Also, are there any memory / performance implications?

谢谢!

推荐答案

如果你把它存储在 $ rootScope ,您将能够在任何控制器,而无需进读它 $ rootScope 由于原型继承,但每分钟有人决定做一个新的 $ scope.user 属性之一你的控制器,你会在 $ rootScope 失去你参考用户属性,除非你有明确的注入和参考 $ rootScope 直接。这可能会浪费别人的时间去调试。

If you store it on $rootScope, you will be able to read it in any controller without injecting $rootScope due to prototypical inheritance, but the minute someone decides to make a new $scope.user property in one of your controllers, you'll lose your reference to the user property on $rootScope, unless you have explicitly injected and reference $rootScope directly. That could waste someone's time trying to debug.

我相信惯例是,它是可以接受的存储在 $ rootScope 数据,但不存储功能存在。我不会从子作用域依赖于原型继承,除非在最低限度,你就 $ rootScope 模型对象与由控制器被覆盖最小的机会,像 $ rootScope.globalData.user 或东西。

I believe convention is that it's acceptable to store data on $rootScope but don't store functions there. I wouldn't rely on the prototypical inheritance from child scopes unless, at a minimum, you make a model object on $rootScope with a minimal chance of being overridden by a controller, like $rootScope.globalData.user or something.

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

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