有没有办法要求的$ HTTP进行拦截? [英] Is there a way to request $http for an interceptor?

查看:108
本文介绍了有没有办法要求的$ HTTP进行拦截?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样做是为了获得在某些情况下另一个源数据,所以我有这个存根:

 厂(拦截器功能($ Q $位置$ HTTP){
    复位功能(承诺){
        返回的承诺;
    }
}

这失败


  

[$喷油器:CDEP]循环依赖发现:拦截器< - $ HTTP


也试过注入$喷油器和使用检索$ HTTP,具有相同的结果。任何想法?

的.config不过声明:

 的.config(函数($ httpProvider){
    $ httpProvider.responseInterceptors.push('拦截');
});


解决方案

注入 $注射器拦截

使用它来获得 $ HTTP 回调函数中返回的对象中。

下面是一个例如

 的app.config(函数($ httpProvider){
  $ httpProvider.interceptors.push('拦截');
});app.factory(拦截器功能($ Q $位置$喷油器){
  返回{
    要求:功能(配置){
      变量$ HTTP = $ injector.get('$ HTTP');
      console.dir($ HTTP);
      返回配置;
    }
  }
});app.run(函数($ HTTP){
  $ http.get('/')
});

The idea is to get a data from another source in certain cases, so I have this stub:

factory("interceptor", function ($q, $location, $http) {
    return function (promise) {
        return promise;
    }
}

which fails with

[$injector:cdep] Circular dependency found: interceptor <- $http

Also tried to inject $injector and retrieve $http using that, with same results. Any ideas?

.config is nothing but declaration:

.config(function ($httpProvider) {
    $httpProvider.responseInterceptors.push('interceptor');
});

解决方案

Inject $injector to interceptor:

Use it to get $http inside the returned object within callback functions.

Here is an example

app.config(function ($httpProvider) {
  $httpProvider.interceptors.push('interceptor');
});

app.factory("interceptor", function ($q, $location, $injector) {
  return {
    request: function(config){      
      var $http = $injector.get('$http');
      console.dir($http);
      return config;
    }
  }
});

app.run(function($http){
  $http.get('/')
});

这篇关于有没有办法要求的$ HTTP进行拦截?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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