我有一个单独的服务模块,我需要一个服务注入到依赖模块的配置功能中。
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'; }};
});
});
我也创造了一个小提琴:http://jsfiddle.net/FzGmL/
代码会爆炸未知提供商:来自app的myservice
如果从myservice
模块配置函数中删除app
参数,mycontroller
构造函数可以注入myservice
就好了。
如何将myservice
注入app
模块的配置功能?
答案 0 :(得分:6)
您正在寻找Module.run()
方法,它会在注入完成加载后为您执行初始化工作。
angular.module('myApp', ['app.services'])
.run(function(myservice) {
//stuff here.
});