"命名空间"在AngularJS服务 [英] "Namespacing" services in AngularJS
问题描述
如何处理AngularJS服务的名称之间的冲突?例如,如果我宣布每个都包含了一个名为'富'服务两个模块。会是什么,如果我想创建一个可重复使用的模块,或者想避免与其他第三方模块来碰撞命名空间服务的好办法?
How does AngularJS handle collisions between names of services? For example if I have declared two modules each containing a service called 'foo'. What would be a good way to "namespace" services if I want to create a reusable module or want to avoid collisions with other third-party modules?
推荐答案
截至今日AngularJS不处理名称空间冲突的服务,所以如果你有2个不同的模块,命名的服务同样的方式,你在应用程式两个模块,只有一个服务将是可用的。
As of today AngularJS doesn't handle namespace collisions for services so if you've got 2 different modules with the service named the same way and you include both modules in your app, only one service will be available.
有关当下最好的选择是preFIX服务名称与自定义preFIX,例如:
For the moment the best option is to prefix service names with a custom prefix, ex:
angular.module(我的prefix_mymodule',['DEP1','dep2'])。工厂(我的prefix_MyService',...)
这篇关于"命名空间"在AngularJS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!