【问题标题】:Chicken and egg issue with dependency injection in angular, $exceptionHandler and $httpAngular、$exceptionHandler 和 $http 中依赖注入的鸡和蛋问题
【发布时间】:2014-05-06 05:03:03
【问题描述】:

我很享受 Angular 依赖注入带来的鸡和蛋问题,我通过注入 $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
}

【问题讨论】:

  • 请添加jsfiddle或完整代码示例。
  • 我的魔法水晶球告诉我,你忘记在任何你想使用的地方注入 $http
  • @AlexChoroshin 它是一个庞大的应用程序,所以不是真的,它以前以默认和预期方式工作

标签: angularjs


【解决方案1】:

这是一个创建 interceptors 的工作示例。

示例:

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/

【讨论】:

  • 我认为我的应该可以工作,你能检查我的编辑。此代码以前可以工作,但由于某种原因将其部署在新服务器上会导致此问题..
  • 你的代码和我的一样,可能是一些服务器错误
猜你喜欢
  • 2011-11-05
  • 2017-05-29
  • 2014-04-15
  • 2010-11-25
  • 2019-04-02
  • 2023-03-22
  • 2014-05-27
  • 1970-01-01
  • 2013-05-22
相关资源
最近更新 更多