注入依赖成提供商 [英] injecting dependencies into provider
本文介绍了注入依赖成提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在供应商做DI?
Is it possible to do DI in provider?
在这个例子中
angular.module('greet',[])
.provider('greeter',function() {
this.$get=function() {
};
})
.service('greeterService',function($http){
console.log($http);
})
;
$注射到HTTP服务都不错。
injecting $http into service is all good.
但它不提供其抛出未知提供商合作是一样的:$ HTTP
but it doesn't work the same in provider which throws Unknown provider: $http
提供商是否意味着与DI工作?
Does provider mean to work with DI?
感谢。
推荐答案
您当然可以注入 $ HTTP
来提供。只要确保它出现在 $ GET
,不是函数的构造。具体如下:
You can certainly inject $http
to provider. Just make sure it appears in $get
, not the function constructor. As follows:
angular.module('greet',[]).provider('greeter',function() {
this.$get = function($http) {
};
});
这篇关于注入依赖成提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文