鸡和蛋的问题与依赖注入的角度,$ exceptionHandler的和$ HTTP [英] Chicken and egg issue with dependency injection in angular, $exceptionHandler and $http

查看:260
本文介绍了鸡和蛋的问题与依赖注入的角度,$ exceptionHandler的和$ HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很享受在角依赖关系注入一个很好的鸡和蛋的问题,我通过注入$ HTTP覆盖$ exceptionHandler的,但我也有一个自定义的拦截器。

 未捕获的错误:[$喷油器:unpr]未知提供商:$ httpProvider<  -  $ HTTP<  -  $ exceptionHandler的<  -  $ rootScope

我想使用它,像这样:

  Module.config(['$ httpProvider',函数($ httpProvider){
  $ httpProvider.interceptors.push('特殊的HTTP');
}]);

我裹着我的截击机在一家工厂:

  Module.factory('特殊的HTTP',['$ templateCache',函数($ templateCache){
  使用严格的;  返回{
    请求:功能(配置){      如果($ templateCache.get(config.url)){
        返回配置;
      }     //一些逻辑      返回配置;
    }
  }
}]);

exceptionHandler.js

  Module.factory('$ exceptionHandler的',['$ HTTP',函数($ HTTP){
    使用严格的;
// code
}


解决方案

下面是创建的拦截的。

示例:

 的app.config(['$ httpProvider',函数($ httpProvider){
            $ httpProvider.interceptors.push(函数($ Q){
            返回{
             请求:功能(配置){
                 //与上述相同
              },              回应:功能(响应){
                 //与上述相同
              }
            };
          });
    }]);

活生生的例子: http://jsfiddle.net/choroshin/mxk92/

I'm enjoying a nice chicken and egg problem with dependancy injection in angular, I override $exceptionHandler by injecting $http, but I also have a custom interceptor.

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

I am trying to use it like so:

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

I wrapped my interceptor in a factory:

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
}

解决方案

Here's a working example of creating interceptors .

Example:

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

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

Live example: http://jsfiddle.net/choroshin/mxk92/

这篇关于鸡和蛋的问题与依赖注入的角度,$ exceptionHandler的和$ HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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