javascript $rootScope 与服务 - Angular JS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16331102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$rootScope vs. service - Angular JS
提问by jankoichi
What is the difference between implementing a $rootScope function and a service? Security wise or performance wise.
实现 $rootScope 函数和服务有什么区别?安全明智或性能明智。
I have read this, so I am wondering.
我读过这个,所以我想知道。
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 本身上实现。为了让你们了解我在做什么,我目前正在开发一个脏表单功能,如果用户离开某个表单,它会提示用户。在这种情况下,我决定最好将它实现为一个全局函数,所以有什么提示吗?
Thanks for the responses,
感谢您的回复,
Jan
简
回答by Eduard Gamonal
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.
在这种情况下,我会选择一项服务以避免拥有全局状态。所有新作用域都是从 $rootScope 创建的。新控制器或使用范围的任何人都将具有$rootscope
可用值。例如,如果您定义$rootScope.validate()
并在控制器中定义了一个函数,$scope.validate()
因为您忘记了第一个定义,那么肯定会出错。
There is an article by Misko H. about this http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/
Misko H. 有一篇关于这个的文章http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/
Services are instantianted on demand, whereas $rootScope is created during bootstrap, and can be injected wherever you need them. This is good for testability.
服务是按需实例化的,而 $rootScope 是在引导过程中创建的,并且可以在您需要的任何地方注入。这有利于可测试性。
Angular won't instantiate services unless they are requested directly or indirectly by the application.
除非应用程序直接或间接请求服务,否则 Angular 不会实例化服务。
(http://docs.angularjs.org/guide/dev_guide.services.creating_services)
(http://docs.angularjs.org/guide/dev_guide.services.creating_services)
回答by Chandermani
As @egamonal mentioned Services are more robust way to share common functionality. Not only services are instantiated on demand, they are singleton by nature so once a service gets created, the same instance gets passed around by AngularJS whenever requested. So if something can go on root scope, it can also be implemented using a service.
正如@egamonal 提到的,服务是共享通用功能的更强大的方式。不仅服务是按需实例化的,而且它们本质上是单例的,因此一旦创建了服务,AngularJS 就会在请求时传递相同的实例。因此,如果某些事情可以在根范围内进行,那么它也可以使用服务来实现。
One think to keep in mind using such global approach is the possibility on memory leak. Instance of JS classes or maybe DOM elements remain in the memory because you have referenced them from you global functions (either in $rootscope or service).
使用这种全局方法要记住的一个想法是内存泄漏的可能性。JS 类的实例或 DOM 元素仍保留在内存中,因为您已从全局函数(在 $rootscope 或服务中)引用了它们。