4

Why this example not work? jsfiddle I get error what provider not found.

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);
    }]);

1 Answer 1

3

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

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

What you DO have access to in the config function is the service-provider for test so you could do

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

This would typically be if you want to provide some sort of configuration to your test-service that would be specific for the m2 module.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! but ...why? I think there is a difference of service providers, allowing you to access the on configuration step
The configuration step of the lifecycle is there in case you want to provide any kind of configuration to a service before it is available for injection. It is briefly mention in the docs (docs.angularjs.org/api/angular.module) with an example of configuring the hash-prefix for the $location service.
There's also an example here: gist.github.com/Mithrandir0x/3639232. If you wanted your service to say something like "hello from m2" you'd typically set that bit up in the config step.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.