When working on my project, I decided to separate angular services into different files. Each file or service should be a part of a shared module called 'com.mysite.services
'. For example:
- ServiceA.js
- ServiceB.js
- ServiceC.js
But when defining them like this:
angular.module('com.mysite.services', []).
service('ServiceA', function()
{
});
I found that this was overwriting the module for each file. To fix this issue, I created a wrapper function that would check if the module is defined and return a reference to it, or create the module if it didn't exist:
function angular_module(name, deps)
{
var m;
try
{
m = angular.module(name);
}
catch (e)
{
m = angular.module(name, deps || []);
}
return m;
};
So, to address this problem, I simply replaced the "." with "_" in the module declaration:
angular_module('com.mysite.services', []).
service('ServiceA', function()
{
});
This workaround worked for me, but it made me wonder: is there an Angular-friendly solution to avoid using my custom wrapper function?