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?
-
possible duplicate of Modules and name clashes in Angularjsshannon– shannon2015-06-02 09:00:05 +00:00Commented Jun 2, 2015 at 9:00
-
possible duplicate of the earlier-asked/answered stackoverflow.com/questions/13406791/…shannon– shannon2015-06-02 09:02:25 +00:00Commented Jun 2, 2015 at 9:02
2 Answers
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', ...)
9 Comments
factory('myModule1.myService')['$scope', 'myModule1.myService', function($scope, myService) { ...}]As noted by pkozlowski, they don't. You can manually add a prefix to all of your services (which is kind of annoying), alternatively, I wrote a hack to namespace it for you. https://github.com/callmehiphop/angular-namespacer
1 Comment
namespace you might just modify module to do it automatically and even add a module method to what the main module method returns so that you can make sub-modules all nice and chained like! ;)