且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS:如何阻止请求

更新时间:2022-03-21 03:12:35

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

In 1.1.5 and later you can use the 'timeout' property of the configuration object.

来自文档:

timeout – {number|Promise} – 以毫秒为单位的超时时间,或承诺解决后应中止请求.

timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.

简单例子:

$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');