【问题标题】:Angular response interceptor does not work for RestAngular角响应拦截器不适用于 RestAngular
【发布时间】:2014-09-16 21:14:01
【问题描述】:

我有以下代码:

!(function(window, angular){
    'use strict';
    angular.module('interceptor', ['settings']).
        config(function($httpProvider, $injector){
            var $http,
                interceptor = ['$q', '$injector', function ($q, $injector) {
                    var error;
                    function success(response) {
                        var $http = $http || $injector.get('$http');
                        var $timeout = $timeout || $injector.get('$timeout');
                        var $rootScope = $rootScope || $injector.get('$rootScope');
                        var LOADER_CONF = LOADER_CONF || $injector.get('LOADER_CONF');
                        if($http.pendingRequests.length < 1) {
                            $timeout(function(){
                                if($http.pendingRequests.length < 1){
                                    $rootScope._loading = false;
                                }
                            }, LOADER_CONF.SUSPEND_ON);
                        }
                        else{
                            $timeout(function(){
                                if($http.pendingRequests.length > 0){
                                    $rootScope._loading = true;
                                }
                            }, LOADER_CONF.SUSPEND_OFF);
                        }
                        return response;
                    }

                    function error(response) {
                        var $state = $state || $injector.get("$state");
                        var $timeout = $timeout || $injector.get('$timeout');
                        var $rootScope = $rootScope || $injector.get('$rootScope');
                        var LOADER_CONF = LOADER_CONF || $injector.get('LOADER_CONF');
                        $timeout(function(){ $rootScope._loading = false, LOADER_CONF.SUSPEND_OFF});
                        return $q.reject(response);
                    }

                    return function (promise) {
                        return promise.then(success, error);
                    }
                }];

            $httpProvider.responseInterceptors.push(interceptor);
        });
})(window, window.angular);

用于切换微调器的基本拦截器。

拦截器适用于获取部分和内部角度内容的 http 请求,但不适用于 RestAngular 请求(它是 $http 的包装器)。

非常感谢任何帮助。

【问题讨论】:

    标签: angularjs restangular angular-http angular-http-interceptors


    【解决方案1】:

    可能由于承诺,Restangular 拦截不起作用。尝试像这样使用 $q:

    var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {
      return {
        'request': function(config) {
          console.log("request successful");
          return config || $q.when(config);
        },
        'requestError': function(rejection) {
          console.log("request error");
          return $q.reject(rejection);
        },
        'response': function(response) {
          console.log("response successful");
          return response || $q.when(response);
        },
        'responseError' : function(rejection) {
          console.log(rejection.status+" something bad happened");
          $rootScope.errors = ["Unable to connect to server "+rejection.config.url];
    
          return $q.reject(rejection);
        }
      };
    
    }];
    

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 2014-05-29
      • 1970-01-01
      • 2013-11-07
      • 2018-05-17
      • 2018-07-25
      • 1970-01-01
      • 2017-07-28
      • 2021-10-20
      相关资源
      最近更新 更多