[英]Decorating a service with a method that uses $http
I'm trying to decorate a service with another method. 我正在尝试用另一种方法装饰服务。 The problem is that method uses
$http
which I can't inject into the angular.config
block because it hasn't been initialised yet. 问题是该方法使用
$http
,我无法注入angular.config
块,因为它尚未初始化。
I thought I could get around this by using $injector
as this would only run when the method I add gets called, but this results in the error: 我以为我可以通过使用
$injector
解决这个问题,因为这只会在我添加的方法被调用时运行,但这会导致错误:
Error: [$injector:unpr] Unknown provider: $http
Here's an example of what I am trying to do: 这是我想要做的一个例子:
angular.module('someModule', [])
.config(($provide, $injector)->
$provide.decorator('someService', ($delegate)->
$delegate.newMethod = ()->
$http = $injector.get('$http')
$http.get('someURL')
return $delegate
)
)
Later on, only when I call someService.newMethod()
do I get the error mentioned above. 稍后,只有当我调用
someService.newMethod()
才会收到上面提到的错误。
Is there any way to do what I'm trying to do? 有没有办法做我想做的事情?
Apparently the $injector
needs to be injected to the decorator as well, so this will fix it: 显然,
$injector
注入$injector
需要注入装饰器,所以这将解决它:
angular.module('someModule', [])
.config(($provide, $injector)->
$provide.decorator('someService', ($delegate, $injector)->
$delegate.newMethod = ()->
$http = $injector.get('$http')
$http.get('someURL')
return $delegate
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.