修复在覆盖$exceptionHandler中使用$http时的循环依赖关系

Fixing circular dependancy when using $http inside the overriden $exceptionHandler?

本文关键字:http 循环 关系 依赖 覆盖 exceptionHandler      更新时间:2023-09-26

我通过注入$http覆盖了$exceptionHandler。我这样做是因为我将一个 arror 登录到服务器。

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 

您可以注入$injector以便稍后获得$http

Module.factory('$exceptionHandler', ['$injector', function ($injector) {
    "use strict";
    return function (exception, cause) {
        var $http = $injector.get('$http');
        // code
    };
} 

看到这个小提琴