6

I have a module. It has a config block, a provider, and a constant defined. The config block references both the constant and the provider. I notice that my constant can be defined before or after my config block. The provider however must be defined BEFORE the config block or else I get an error.

Error: [$injector:modulerr] Failed to instantiate module loadOrder due to:
[$injector:unpr] Unknown provider: greetingsProvider

Here is some sample code:

var myModule = angular.module('loadOrder', []);

//if I define this after the config block, I get an error
angular.module('loadOrder').provider('greetings',[ function(){
    this.$get = [function(){
        return { greet: function(){ return "Hola"; } };
    }];
}]);

myModule.config(['$provide', 'greetingsProvider', 'planetName', function($provide, loadOrderProvider, planetName){
    $provide.value('someVals',[3,6,8]);
    console.log("Lets go to", planetName);
}]);

myModule.constant('planetName', 'Saturn');

Why is this? Why can't I define my provider after my config block?

Selah
  • 7,728
  • 9
  • 48
  • 60

1 Answers1

0

When you call provider, config or constant nothing happens immediately. The calls are registered, put in a queue and run during the initialization of the application.

The funny thing with constant is that it is put at the front of the queue. Thus it's available before config, no matter what.

a better oliver
  • 26,330
  • 2
  • 58
  • 66
  • I'm still confused because if I reference a service in my run block, it doesn't cause an error if the service is defined after the run block. Do you know why the definition order doesn't matter for for services/run blocks but it does matter for providers/config blocks? – Selah Apr 16 '14 at 20:20
  • @Selah run blocks are called after everything has been set up. – a better oliver Apr 16 '14 at 20:31
  • @zeroflagL Can you clarify why the provider has to DEFINED before the config block. If nothing happens immediately, why would there be an error if the provider is defined after the config block, but not if it is defined before? – dnc253 Feb 12 '15 at 16:27
  • @dnc253: All functions eventually will be called and they are called in the order they have been registered. Of course, it makes no sense that `config` functions are run before `provider` functions, or even put in the same queue, to start with, but that's how it currently works. – a better oliver Feb 13 '15 at 08:39