2

我有以下代码:

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

任何帮助深表感谢。

4

1 回答 1

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);
    }
  };

}];
于 2014-10-28T17:25:27.307 回答