【问题标题】:Fixing circular dependancy when using $http inside the overriden $exceptionHandler?在覆盖 $exceptionHandler 中使用 $http 时修复循环依赖?
【发布时间】:2014-03-31 07:41:02
【问题描述】:

我通过注入 $http 覆盖了 $exceptionHandler。我这样做是因为我在服务器上记录了一个错误。

Module.factory('$exceptionHandler', ['$http', function ($http) {
    "use strict";
//code
} 

但是我也有一个拦截器来添加我的 $httpProvider:

Module.config(['$httpProvider', function($httpProvider) {
      $httpProvider.interceptors.push('special-http');
    }]);

这是拦截器(我包装在工厂中):

Module.factory('special-http', ['$templateCache', function($templateCache) {
  "use strict";

  return {
    'request': function(config) {

      if ($templateCache.get(config.url)){
        return config;
      }

     //some logic

      return config;
    }
  }
}]);

EDIT___________________________

修改'$http'的名字后

我现在收到此错误:

Uncaught Error: [$injector:cdep] Circular dependency found: $http <- $exceptionHandler <- $rootScope
http://errors.angularjs.org/1.2.1/$injector/cdep?p0=%24http%20%3C-%20%24exceptionHandler%20%3C-%20%24rootScope 

【问题讨论】:

  • 这可能是一件很傻的事情,但工厂定义中的字符串“$http”中没有空格吗?
  • @Wawy 如果是我会脸红..
  • @Wawy 谢谢,你能看看我关于循环依赖的编辑吗
  • http 已经依赖于 exceptionHandler,你不能将 http 注入到 exceptionHandler 中。
  • @mpm 啊..所以任何想法我需要什么方法在错误时登录到服务器?

标签: javascript angularjs


【解决方案1】:

以后可以注入$injector获取$http:

Module.factory('$exceptionHandler', ['$injector', function ($injector) {
    "use strict";

    return function (exception, cause) {
        var $http = $injector.get('$http');
        // code
    };
} 

见this fiddle

【讨论】:

  • 我很确定您会遇到完全相同的问题。
  • 不,我已经在生产环境中运行了几个月 :)
  • @khillang 令人印象深刻,非常感谢克里斯蒂安!快乐的角度编码!
  • 看起来 always 在 1.2.0 中重命名为 finally in this commit :)
  • @fatmatto:感谢您的评论。这对我帮助很大。
猜你喜欢
  • 2016-10-02
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 2014-05-07
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多