相关疑难解决方法(0)

有没有办法为拦截器请求$ http?

这个想法是在某些情况下从另一个来源获取数据,所以我有这个存根:

factory("interceptor", function ($q, $location, $http) {
    return function (promise) {
        return promise;
    }
}
Run Code Online (Sandbox Code Playgroud)

失败了

[$ injector:cdep]发现循环依赖:拦截器< - $ http

还试图注入$ injector并使用它来检索$ http,结果相同.有任何想法吗?

.config只是声明:

.config(function ($httpProvider) {
    $httpProvider.responseInterceptors.push('interceptor');
});
Run Code Online (Sandbox Code Playgroud)

angularjs

5
推荐指数
1
解决办法
4084
查看次数

将$ route注入http拦截器

我正在尝试为每个API请求注入自定义标头.当我提供一些硬编码文本时,它可以工作.

工作代码

myApp.config(['$httpProvider', function ($httpProvider) {
    var requestInterceptor = ['$q', '$rootScope', 
        function ($q, $rootScope) {
            var interceptorInstance = {
                request: function (config) {
                    config.headers['X-MyApp-CustomHeader'] = "foobar"; 
                    return config || $q.when(config);
                }
            };
            return interceptorInstance;
        }];

    $httpProvider.interceptors.push(requestInterceptor);
}]);
Run Code Online (Sandbox Code Playgroud)

不工作的代码

myApp.config(['$httpProvider', function ($httpProvider) {
    var requestInterceptor = ['$q', '$rootScope', '$route',
        function ($q, $rootScope, $route ) {
            var interceptorInstance = {
                request: function (config) {
                    config.headers['X-MyApp-CustomHeader'] = $route.current.params.CustomParameter; 
                    return config || $q.when(config);
                }
            };
            return interceptorInstance;
        }];

    $httpProvider.interceptors.push(requestInterceptor);
}]);
Run Code Online (Sandbox Code Playgroud)

错误,尝试注入时$route

未捕获错误:[$ …

angularjs angular-http-interceptors

5
推荐指数
1
解决办法
1万
查看次数