“命名空间"AngularJS 中的服务 [英] "Namespacing" services in AngularJS

查看:27
本文介绍了“命名空间"AngularJS 中的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS 如何处理服务名称之间的冲突?例如,如果我声明了两个模块,每个模块都包含一个名为foo"的服务.如果我想创建一个可重用的模块或想避免与其他第三方模块发生冲突,什么是命名空间"服务的好方法?

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.

目前最好的选择是使用自定义前缀作为服务名称的前缀,例如:

For the moment the best option is to prefix service names with a custom prefix, ex:

angular.module('myprefix_mymodule',['dep1', 'dep2']).factory('myprefix_MyService', ...)

这篇关于“命名空间"AngularJS 中的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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