AngularJS:DRY$http.error()

AngularJS:DRY$http.error(),angularjs,Angularjs,因此,我有一组控制器来处理$http请求 但是在每个$http请求中,我都有一个.error(函数(数据…{//always same}) 我怎样才能建立一个。。$http的“抽象类” 这将是始终重复的代码 .error(function(){ $scope.flashes = { server: { type: "danger", message: "There was a server error processing

因此,我有一组控制器来处理$http请求

但是在每个$http请求中,我都有一个
.error(函数(数据…{//always same})

我怎样才能建立一个。。$http的“抽象类”

这将是始终重复的代码

.error(function(){
    $scope.flashes = {
        server: {
            type: "danger",
            message: "There was a server error processing your request. Please try again later."
        }
    };
})

你可以这样做:

app.service('myHttp', function($http){
  return function($scope, httpParameters){
    var httpPromise = $http(httpParameters);
    httpPromise.error(function(){
      $scope.flashes = {
         server: {
            type: "danger",
            message: "There was a server error"
         }
      }
    });
  };
});

app.controller('MainCtrl', function($scope, myHttp) {
   myHttp($scope, {method: 'GET', url: 'www.google.com'});
});

几周前,我提出了同样的问题,并提出了以下解决方案:

我首先创建了一个自定义服务,拦截每个http请求:

.factory('HttpInterceptor', ['$q', '$rootScope', function($q, $rootScope) {
return {
            // On request success
            request : function(config) {
                // Return the config or wrap it in a promise if blank.
                return config || $q.when(config);
            },

            // On request failure
            requestError : function(rejection) {
                //console.log(rejection); // Contains the data about the error on the request.  
                // Return the promise rejection.
                return $q.reject(rejection);
            },

            // On response success
            response : function(response) {
                //console.log(response); // Contains the data from the response.
                // Return the response or promise.
                return response || $q.when(response);
            },

            // On response failure
            responseError : function(rejection) {
                //console.log(rejection); // Contains the data about the error.
                //Check whether the intercept param is set in the config array. If the intercept param is missing or set to true, we display a modal containing the error
                if (rejection.config && typeof rejection.config.intercept === 'undefined' || rejection.config.intercept)
                {
                    //emitting an event to draw a modal using angular bootstrap
                    $rootScope.$emit('errorModal', rejection.data);
                }

                // Return the promise rejection.
                return $q.reject(rejection);
            }
        };
}]);
我还定义了一个自定义配置属性“intercept”,可以将其添加到$http config对象中。当我不想将此行为应用于特定请求时,它非常有用。 例如:

为了获得灵活的解决方案,不要忘记执行以下操作也很重要:

 return $q.reject(rejection);
因此,如果您想将这两种方式结合起来(拦截+手动处理),那么您仍然可以在控制器中对您的承诺使用错误回调

最后,我将此服务添加到我的应用程序中:

app.config(['$httpProvider', function($httpProvider) {
        // Add the interceptor to the $httpProvider to intercept http calls
        $httpProvider.interceptors.push('HttpInterceptor');

    }]);
我简化了服务,但你也可以用它做很多事情。就我个人而言,我也用它来:

  • 确保不要触发重复的http请求(如果用户在提交按钮上单击了很多次)

  • 在http调用开始时绘制警报,并在结束时关闭警报,以通知用户正在处理(例如数据导出)

附:官方文件提到了这一点

app.config(['$httpProvider', function($httpProvider) {
        // Add the interceptor to the $httpProvider to intercept http calls
        $httpProvider.interceptors.push('HttpInterceptor');

    }]);