angularjs中的500 http状态路由

angularjs中的500 http状态路由,angularjs,Angularjs,我有一个angularjs应用程序,我正在使用UIRouter进行路由 我的状态如下: .state('core.page500', { url: '/page500', templateUrl: 'views/tmpl/pages/page500.html' }) 我希望每当某个api调用在我的应用程序中返回500作为HTTP状态时,将我路由到该状态 这在AngularJS中可能吗?是的,可能。 你需要做的是 创建一个自定义拦截器工厂,该工厂检查r

我有一个angularjs应用程序,我正在使用
UIRouter
进行路由

我的状态如下:

.state('core.page500', {
        url: '/page500',
        templateUrl: 'views/tmpl/pages/page500.html'
      })
我希望每当某个api调用在我的应用程序中返回500作为HTTP状态时,将我路由到该状态

这在AngularJS中可能吗?

是的,可能。 你需要做的是 创建一个自定义拦截器工厂,该工厂检查response.status并在status==500时运行重路由代码,然后将其附加到angularJS http拦截器

文件:$http

尝试在谷歌上搜索angularJS自定义http拦截器的详细实现。

是的,这是可能的。 你需要做的是 创建一个自定义拦截器工厂,该工厂检查response.status并在status==500时运行重路由代码,然后将其附加到angularJS http拦截器

文件:$http


尝试在谷歌上搜索angularJS自定义http拦截器以获得详细的实现。

您可以使用http拦截器。我还没有测试过这个,所以如果它不起作用,请告诉我,但这非常接近您所需要的:

angular.module('YourModule').factory('errorHttpInterceptor', ['$injector', '$q', '$state', errorHttpInterceptor]);

    function errorHttpInterceptor($injector, $q, $state) {
        return {
            responseError: function (error) {
                if (error.status === 500) {
                   $state.go('core.page500');
                }
                return $q.reject(error);
            }
        };
    }
然后将其添加到http管道中:

angular.module('YourModule').config(['$httpProvider', config]);

function config($httpProvider) {
   $httpProvider.interceptors.push('errorHttpInterceptor');
}

这将捕获所有500个错误并将用户发送到您的错误页面。

您可以使用Http拦截器。我还没有测试过这个,所以如果它不起作用,请告诉我,但这非常接近您所需要的:

angular.module('YourModule').factory('errorHttpInterceptor', ['$injector', '$q', '$state', errorHttpInterceptor]);

    function errorHttpInterceptor($injector, $q, $state) {
        return {
            responseError: function (error) {
                if (error.status === 500) {
                   $state.go('core.page500');
                }
                return $q.reject(error);
            }
        };
    }
然后将其添加到http管道中:

angular.module('YourModule').config(['$httpProvider', config]);

function config($httpProvider) {
   $httpProvider.interceptors.push('errorHttpInterceptor');
}

这将捕获所有500个错误,并将用户发送到您的错误页面。

请看一个名为的概念

您基本上是在创建一个服务,该服务在每次$http调用时都会被触发。情况如下:

 angular
  .module( 'interceptors', [])
  .factory('HttpInterceptors', HttpInterceptors);

HttpInterceptors.$inject = ['$q', '$injector'];

function HttpInterceptors($q, $injector) {
  return {
    // On request success
    request: function (config) {
      // console.log(config); // Contains the data about the request before it is sent.

      // 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', 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 failture
    responseError: function (rejection) {
      if(rejection.status === 500) {
        var state = $injector.get('$state');
        state.go('core.page500');
      }

      // Return the promise rejection.
      return $q.reject(rejection);
    }
  };
}
然后,将其推送到应用程序.config部分的
$routeProvider
中:

$httpProvider.interceptors.push('HttpInterceptors');

请看一个名为的概念

您基本上是在创建一个服务,该服务在每次$http调用时都会被触发。情况如下:

 angular
  .module( 'interceptors', [])
  .factory('HttpInterceptors', HttpInterceptors);

HttpInterceptors.$inject = ['$q', '$injector'];

function HttpInterceptors($q, $injector) {
  return {
    // On request success
    request: function (config) {
      // console.log(config); // Contains the data about the request before it is sent.

      // 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', 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 failture
    responseError: function (rejection) {
      if(rejection.status === 500) {
        var state = $injector.get('$state');
        state.go('core.page500');
      }

      // Return the promise rejection.
      return $q.reject(rejection);
    }
  };
}
然后,将其推送到应用程序.config部分的
$routeProvider
中:

$httpProvider.interceptors.push('HttpInterceptors');