AngularJS:如何阻止请求

Flo*_*n F 7 request angularjs

是否可以使用angularjs拦截器阻止请求?

$provide.factory('myHttpInterceptor', function($q, someService) {
  return {
    'request': function(config) {
      // here I'd like to cancel a request depending of some conditions
    }
  }
});

$httpProvider.interceptors.push('myHttpInterceptor');
Run Code Online (Sandbox Code Playgroud)

tas*_*ATT 16

在1.1.5及更高版本中,您可以使用配置对象的"timeout"属性.

文档:

timeout - {number | Promise} - 超时(以毫秒为单位),或承诺在解决时应中止请求.

简单的例子:

$provide.factory('myHttpInterceptor', function($q, someService) {
  return {
    'request': function(config) {

        var canceler = $q.defer();

        config.timeout = canceler.promise;

        if (true) {

            // Canceling request
            canceler.resolve();
        }

        return config;
    }
  }
});

$httpProvider.interceptors.push('myHttpInterceptor');
Run Code Online (Sandbox Code Playgroud)