Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Namespacing" services in AngularJS

Tags:

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?

like image 343
Esa Toivola Avatar asked Feb 16 '13 10:02

Esa Toivola


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', ...)

like image 95
pkozlowski.opensource Avatar answered Sep 25 '22 04:09

pkozlowski.opensource


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

like image 26
callmehiphop Avatar answered Sep 22 '22 04:09

callmehiphop