将 $http 注入 angular factory($exceptionHandler) 会导致循环依赖 [英] Injecting $http into angular factory($exceptionHandler) results in a Circular dependency

查看:26
本文介绍了将 $http 注入 angular factory($exceptionHandler) 会导致循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将 $http 注入被覆盖的工厂时,出现错误:

When I try inject $http into an overridden factory I get the error:

未捕获的错误:[$injector:cdep] 找到循环依赖:$http <-$exceptionHandler <- $rootScope

Uncaught Error: [$injector:cdep] Circular dependency found: $http <- $exceptionHandler <- $rootScope

AngularModule.factory('$exceptionHandler',  function ($http) {

任何想法如何解决?如果我使用 [] 注入,则 $http 未定义

any ideas how to resolve? if I inject using [], $http is undefined

edit__________________

edit__________________

根据我尝试的以下答案:

as per an answer below I tried:

MyModule.config(function($provide, $http) {
    $provide.decorator("$exceptionHandler", function($delegate) {
        return function(exception, cause) {..

但我仍然收到循环错误:

but I still get the circular error:

未捕获的错误:[$injector:cdep] 找到循环依赖:$http <-$exceptionHandler <- $rootScope

Uncaught Error: [$injector:cdep] Circular dependency found: $http <- $exceptionHandler <- $rootScope

推荐答案

注入 $injector 然后从那里获取 $http 服务.像这样:

Inject the $injector and then get the $http service from there. Something like this:

AngularModule.factory('$exceptionHandler',  function ($injector) {
    var $http = $injector.get("$http");

请参阅 https://groups.google.com/forum/#!topic/angular/lbFY_14ZtnU/讨论

然而,这将完全覆盖 Angular 提供的 $exceptionHandler 功能.如果您只想将服务器端日志添加到现有功能中,请参阅这个问题 关于增强 $exceptionHandler 功能.

However, this will completely override the $exceptionHandler functionality provided by Angular. If you just want to add the server-side log to the existing functionality, see this question about augmenting $exceptionHandler functionality.

这篇关于将 $http 注入 angular factory($exceptionHandler) 会导致循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆