繁体   English   中英

鸡肉和鸡蛋问题与angular,$ exceptionHandler和$ http中的依赖项注入

[英]Chicken and egg issue with dependency injection in angular, $exceptionHandler and $http

我正在通过角度注入依赖项来享受很好的鸡肉和鸡蛋问题,我通过注入$ http覆盖了$ exceptionHandler,但是我也有一个自定义的拦截器。

Uncaught Error: [$injector:unpr] Unknown provider:  $httpProvider <-  $http <- $exceptionHandler <- $rootScope

我正在尝试像这样使用它:

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;
    }
  }
}]);

exceptionHandler.js

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

这是创建拦截器的工作示例。

例:

app.config(['$httpProvider', function($httpProvider) {
            $httpProvider.interceptors.push(function($q) {
            return {
             'request': function(config) {
                 // same as above
              },

              'response': function(response) {
                 // same as above
              }
            };
          }); 
    }]);

实时示例: http//jsfiddle.net/choroshin/mxk92/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM