How to add a provider from another module in Angular.js

Why does this example not work? jsfiddle I get an error that the provider did not find.

var m1 = angular.module('m1', [])
    .provider('test', function() {
        return {
            $get: function() {
                return 'Hello from provider';
            }
        }
     });

var m2 = angular.module('m2', ['m1'])
    .config(['test', function(test) {
        alert(test);
    }]);
+4
source share
1 answer

In the function configyou do not have access to it, try using the method run.

var m2 = angular.module('m2', ['m1'])
  .run(['test', function(test) {
    alert(test);
  }]);

You have access to a function config, this is a service provider for testyou to do

var m2 = angular.module('m2', ['m1'])
  .config(['testProvider', function(test) {
    alert(test);
  }]);

This will usually be the case if you want to provide some configuration to your test service, which will be specific to the m2 module.

+3
source

All Articles