I have a separate services module and I need a service injected into the config function of a dependent module.
angular.module('app', ['app.services'], function(myservice) {/*use myservice*/})
.controller('mycontroller', function($scope, myservice) {
$scope.message = myservice.sayHello();
});
angular.module('app.services', [], function($provide) {
$provide.service('myservice', function() {
return {sayHello: function() { return 'hello'; }};
});
});
I've also create a fiddle: http://jsfiddle.net/FzGmL/
The code will blow up with Unknown provider: myservice from app
If you remove the myservice
argument from the app
module config function, the mycontroller
constructor function is able to have myservice
injected just fine.
How can myservice
be injected into the config function of the app
module?
You're looking for the Module.run()
method, It does initialization work for you after the injector is done loading.
angular.module('myApp', ['app.services'])
.run(function(myservice) {
//stuff here.
});