angularjsangularjs-serviceangularjs-module

Angular add service to multiple modules at once


I have a module that has a service defined as

angular.module('mean.system').service('helperService', ['$rootScope', '$q', '$log', function ($rootScope, $q, $log) {

I am defining another module that will use the same service. I want to inject this service into another module, is it valid to do something like

angular.module('mean.system', 'myModule2').service('helperService', ... (etc) ) {

Solution

  • In that case you should identify and separate all the shared parts of your app (services, controllers, directives, etc) and put them in their own module:

    angular.module('shared.services').service('helperService', ... (etc) )...
    

    And then inject that shared module in the modules that need them:

    angular.module('mean.system', ['shared.services', other-dependencies...])
    angular.module('myModule2', ['shared.services', other-dependencies...])
    

    This way you can use the helperService (and all other parts added to the shared.services module) in your other modules.