AngularJS GlobalCtrl VS $ rootScope VS服务 [英] AngularJS GlobalCtrl vs $rootScope vs Service

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

问题描述

我在角几件事情与全局相混淆。下面是我的伪code。

1)随着我有我的GlobalCtrl放置的方式,我能够从我所有的控制器引用我的$ scope.modalOptions。既然如此,我很困惑,为什么我看到人们将全局属性$ rootScope,而不是仅仅将他们像我在这里做。那是以防万一,他们希望它注入到服务什么的?

2)我应该使用一个服务,而不是增加属性和方法到我的GlobalCtrl?如果是这样,为什么?


 < D​​IV NG-应用=应用程序NG控制器=GlobalCtrl>
    < D​​IV NG-视图>< / DIV>
< / DIV>


 函数GlobalCtrl($范围,$位置){
    $ scope.modalOptions = {
        backdropFade:真实,
        dialogFade:真
    };
}


解决方案

主控制器的方式肯定是preferable使用$ rootScope。

范围继承是存在的,那么为什么不利用它。在我看来,该解决方案适用于大多数情况下,即除非你需要有一个并行控制器的地方(这不会是主要的孩子)。在这种情况下,去最好的办法是使用一个服务和它注入在需要的地方。服务(或者更确切地说,工厂,因为这是你可能会使用什么 - 了解更多关于他们的这里)是单独且在控制器之间共享数据的工作。

重要的是了解作用域

范围继承是在作怪pretty很多常规的JavaScript继承。你应该倾向于使用目标作为您的数据,因为它们是通过引用传递。

如果你有一个原始的像 $ scope.myString ='是一个基本数据类型;在父控制器,并尝试在儿童控制器覆盖值,结果不会是你所期望的 - 它会创建子控制器,而不是书面形式向母公司上一个新的字符串

建议这里阅读

最后的想法

如果您正在使用嵌套控制器的做法,不要忘了还注入 $范围(和其他依赖)在子控制器。它可能没有工作,但它的速度较慢,难以测试,以及最后但并非最不重要 - 错误的方式做到这一点。

最后,如果你有很多状态变量的跟踪和/或大量的使用点,这绝对是一个好主意,他们提取到的服务。

I am confused on a couple of things with Globals in Angular. Below is my pseudo code.

1) With the way I have my GlobalCtrl placed, I am able to reference my $scope.modalOptions from all of my controllers. That being the case, I'm confused as to why I see people adding global properties to $rootScope instead of just adding them like I am doing here. Is that just in case they want to inject it into a service or something?

2) Should I be using a service instead of adding properties and methods to my GlobalCtrl? If so, why?

<div ng-app="app" ng-controller="GlobalCtrl">
    <div ng-view></div>
</div>

function GlobalCtrl($scope, $location) {
    $scope.modalOptions = {
        backdropFade: true,
        dialogFade: true
    };
}

解决方案

The 'Main Controller' approach is definitely preferable to using $rootScope.

Scope inheritance is there, so why not leverage it. In my opinion, that solution works well for most cases, i.e. unless you need to have a parallel controller somewhere (that wouldn't be a child of Main). In that case, the best way to go is to use a service and inject it where needed. Services (or rather factories, because that's what you'll probably be using -- read more about them here) are singletons and work well for sharing data across controllers.

Important to know about scopes

Scope inheritance is pretty much regular JavaScript inheritance at play. You should tend to use objects for your data, because they are passed by reference.

If you have a primitive like $scope.myString = 'is of a primitive data type'; in your parent controller and try to overwrite the value in a child controller, the result won't be what you'd expect -- it will create a new string on the child controller instead of writing to the parent.

Suggested reading here

Final thoughts

If you are using the nested controllers approach, do not forget to still inject $scope (and other dependencies) in the child controller. It might work without, but it's slower and hard to test, and last but not least -- the wrong way to do it.

Finally, if you have a lot of state variables to keep track of and/or a lot of usage points, it's definitely a good idea to extract them into a service.

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

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