角度响应拦截器不适用于RestAngular

时间:2014-09-16 21:14:02

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

我有以下代码:

!(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的包装)。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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);
    }
  };

}];
相关问题