itachi itachi - 5 months ago 33
Javascript Question

AngularJS custom provider causing "Unknown provider" exception

I have created a custom provider in AngularJS, which is supposed to be an application global configuration. Unfortunately, I am not able to inject this provider, because AngularJS throws "Unknown provider" exception. I have totally no idea what is wrong with my code.

app.js



(function () {
'use strict';

angular.module('app', [
'config'
])
.config(configuration);

configuration.$inject = ['ConfigProvider'];

function configuration(ConfigProvider) {
ConfigProvider.setFoo(86400);
}
})();


config-provider.js



(function () {
'use strict';

angular.module('config')
.provider('ConfigProvider', ConfigProvider);

ConfigProvider.$inject = [];

function ConfigProvider() {
var config = [];

var provider = {
$get: $get,
setFoo: setFoo,
};

return provider;

function $get() {
return {
getConfig: function () {
return config;
}
};
}

function setFoo(foo) {
config['foo'] = foo;
}
}
})();


config-provider.js is loaded first in my scripts file, but changing the order does not change the behavior anyway - still "Unknown provider". Any help will be appreciated.

Answer

You should declare the provider without 'Provider' suffix:

module.provider('Config', ConfigProvider);
Comments