Javascript 如何将所需模块中的服务注入模块配置函数?
我有一个单独的服务模块,我需要一个注入到依赖模块的配置函数中的服务Javascript 如何将所需模块中的服务注入模块配置函数?,javascript,angularjs,Javascript,Angularjs,我有一个单独的服务模块,我需要一个注入到依赖模块的配置函数中的服务 angular.module('app', ['app.services'], function(myservice) {/*use myservice*/}) .controller('mycontroller', function($scope, myservice) { $scope.message = myservice.sayHello(); }); angular.module('app.servi
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'; }};
});
});
我还创作了一把小提琴:
代码将被未知的提供者炸毁:来自应用程序的myservice
如果从app
module config函数中删除myservice
参数,则mycontroller
构造函数可以很好地插入myservice
如何将
myservice
注入app
模块的配置功能?您正在寻找module.run()
方法,它在注入器加载完成后为您进行初始化工作
angular.module('myApp', ['app.services'])
.run(function(myservice) {
//stuff here.
});
看那个。就在文件里。谢谢