0

i try to inject a custom service into the module config function, but i get an error that the service is undefined:

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

myApp.config(function($httpProvider, CommonConfigService) {

    var baseUrl = CommonConfigService.getRESTUrl();

});

I use this within a couple of controllers without any problem.

Can someone hint me how to solve this?

Thanks and kind regards

1
  • is the service is injected in angular.module('myApp.services') Commented Oct 7, 2014 at 8:19

2 Answers 2

1

You cannot inject services to a config acording to angular docs ,

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

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

Comments

0

It is possible if service was created as provide. Instead of:

app.factory('MyFactory', function(){});

Use

app.provider('MyFactory', function(){});

Then you can inject it like so:

app.config(function($httpProvider, MyFactoryProvider) {
    var baseUrl = MyFactoryProvider.getRESTUrl();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.