1

I'm failing to inject a AngularJS Provider, called UserAgent inside a .config function but I'm getting this error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:unpr] Unknown provider: UserAgent

This is my config:

angular
    .module('app')
    .config(Config);

Config.$inject = ['$routeProvider', 'UserAgent'];

function Config($routeProvider, UserAgent) {
    /*** code here ***/

}

And this is my provider:

angular
    .module('app')
    .provider('UserAgent', UserAgent);

    function UserAgent() {
        return {
            $get: function () {
                return {
                    title: "Testing..."
                };
            }
        };
    }
Matheus Lima
  • 2,103
  • 3
  • 31
  • 49

3 Answers3

3

Change 'UserAgent' in the following line to 'UserAgentProvider'....

Config.$inject = ['$routeProvider', 'UserAgent'];

Config.$inject = ['$routeProvider', 'UserAgentProvider'];

that's how angular expects providers exposed by apps to be referred as in dependent apps: <ActualProviderName>Provider, the word 'Provider' appended to the actual name of the provider.

Community
  • 1
  • 1
pranay kumar
  • 404
  • 3
  • 8
2

Sorry to necro this question, but I struggled with this same issue and luckily found a solution. I'm no Angular expert so my solution might be done a better way, but hope this helps someone out there.

Looking at AngularJS' documentation for providers , their example states:

myApp.provider('unicornLauncher', function UnicornLauncherProvider() {});

And then they show how to inject that provider into a config function:

myApp.config(["unicornLauncherProvider", function(unicornLauncherProvider) {});

The important thing to note is the naming convention: The provider name doesn't have the word 'Provider' in it at the moment of declaration, yet the config injection does.

Like you, I'm also using a named function for my config. So my config injection declaration looked like:

angular.module('sample').config(['UserAgentProvider', config]);

However, you are doing it slightly different by using the .$inject injector. So your end solution would look like:

angular.module('sample').config(config);
config.$inject = ['UserAgentProvider'];

I too was using the .$inject injector, but switched to the AngularJS documentation style syntax because of this:

Notice that the unicorn provider is injected into the config function. This injection is done by a provider injector which is different from the regular instance injector, in that it instantiates and wires (injects) all provider instances only.

Not sure if the .config(['MyNamedProvider', NamedConfigFunction]) is better vs. NamedConfigFunction.$inject = ['MyNamedProvider'];

But the important point is leaving off the 'Provider' during the declaration of the provider, but adding it back during the injection of said provider

TSmith
  • 543
  • 5
  • 16
0

You may not use services during the configuration phase. The configuration phase is used to configure the providers of services. Once it's done, the providers are called to create the services.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255