So I'm trying to organize custom directives/services that i use in my apps in a separate file which I can include in the file where my controller reside whenever I write a new app/s. This should allow me to use the services across apps & controllers just by including them like other js files. Currently I'm using this way:
angular.module('commonModule',[]).service('myService',function(){});
To include this service in another app, I do - angular.module('anotherModule',['commonModule'])
besides including the js file that has the 'commonModule' service.
However is this the correct method? And If I have dependencies between the services which I have abstracted into a separate file, will they still work in the above arrangement?
I tried the interservices dependencies like this :
angular.module('commonModule',[]).service('myService1',function(){});
angular.module('commonModule',[]).service('myService2',function(myService1){});
The above is failing. what would be a better way?
------------------- UPDATE: As Sten pointed I was redeclaring the module. I have corrected the same. Now I'm doing it as way below however I'm not getting a consistent result. I still get dependency errors on one App while it works for another, both including the same 'commonModule' in their module dependency. Is this an expected behavior/a better way out there? I'm still trying to figure out. Will update whatever it turns out to be.
var commonModule = angular.module('commonModule',[]);
commonModule.service('myService1', function(){});
commonModule.service('myService2', function(myservice1){});
UPDATE 2
So the above approach works. The error I was getting along was an error on my part. So along with the declaration for commonModules as above, I could include them in my apps as below.
angular.module('newModule',['commonModule']);
While this works for now. I would still point some problems with the above approach which leaves the door open for something better, cleaner.
If in the above approach your services depend upon some dependencies like- if you are making an upload service, you will have to add fileupload module in the main file (in my case I used angular-file-upload). Or module like ngSanitize are required.
So if you have services which have dependencies, you cant just do a simple plug & play with the above approach. In larger, multiple apps this will be a problem, in case you miss out.