我有一个单独的服务模块,我需要将服务注入(inject)到依赖模块的配置函数中。
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'; }};
});
});
我还创建了一个 fiddle :http://jsfiddle.net/FzGmL/
代码将因 Unknown provider: myservice from app 而爆炸
如果从 app
模块配置函数中删除 myservice
参数,mycontroller
构造函数就可以拥有 myservice
注入(inject)就好了。
myservice
如何注入(inject)到app
模块的config函数中?
最佳答案
您正在寻找 Module.run()
方法,它会在注入(inject)器完成加载后为您完成初始化工作。
angular.module('myApp', ['app.services'])
.run(function(myservice) {
//stuff here.
});
关于javascript - 如何将所需模块的服务注入(inject)模块配置函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12964660/