$ rootScope与服务 - 角JS [英] $rootScope vs. service - Angular JS

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

问题描述

什么是实现$ rootScope功能和服务之间的区别?明智的安全或性能明智的。

What is the difference between implementing a $rootScope function and a service? Security wise or performance wise.

我已阅读的,所以我想知道。

I have read this, so I am wondering.

我一直在试图找出是否不适合我的应用程序一定的全局函数将在服务上或$ rootScope本身最好的实现。在球场你们什么我做,我目前正在开发中,它会提示用户,如果他/她从导航某种形式离开一个肮脏的形式功能的想法。在这种情况下,我决定最好的实现它作为一个全球性的功能,所以任何提示?

I have been trying to figure out whether or not a certain global function for my app would be best implemented in a service or on the $rootScope itself. To pitch you guys with an idea of what I am making, I'm currently developing a dirty form function in which it prompts the user if he/she navigates away from a certain form. In this case, I decided to best implement it as a global function, so any hints?

感谢您的答复,

推荐答案

在这种情况下,我会去服务,以避免全球性的状态。所有新的范围从$ rootScope创建。新型控制器或谁使用范围将有 $ rootscope 可用的值。例如,如果你定义 $ rootScope.validate()并在控制器中定义一个函数 $ scope.validate()因为你忘了第一个定义,东西肯定会出问题。

In this case I would go for a service to avoid having a global state. all new scopes are created from $rootScope. New controllers or whoever uses a scope will have values of $rootscope available. For instance, if you define $rootScope.validate() and in a controller you define a function $scope.validate() because you forget about the first definition, something will certainly go wrong.

有是MISKO H.这个<一的一篇文章href=\"http://misko.hevery.com/$c$c-reviewers-guide/flaw-brittle-global-state-singletons/\">http://misko.hevery.com/$c$c-reviewers-guide/flaw-brittle-global-state-singletons/

There is an article by Misko H. about this http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/

服务是instantianted需求,而引导过程中创建$ rootScope,何地你需要他们可以注射。这是良好的可测试性。

Services are instantianted on demand, whereas $rootScope is created during bootstrap, and can be injected wherever you need them. This is good for testability.

角不会实例服务

(<一个href=\"http://docs.angularjs.org/guide/dev_guide.services.creating_services\">http://docs.angularjs.org/guide/dev_guide.services.creating_services)

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

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