AngularJS:如何prevent请求 [英] AngularJS : How to prevent a request

查看:146
本文介绍了AngularJS:如何prevent请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能prevent使用angularjs拦截器的请求?

  $ provide.factory('myHttpInterceptor',函数($ Q,someService){
  返回{
    请求:功能(配置){
      //这里我想取消这取决于一些条件的要求
    }
  }
});$ httpProvider.interceptors.push('myHttpInterceptor');


解决方案

在1.1.5以后,你可以使用配置对象的超时属性。

文档


  

超时 - {号|无极} - 超时以毫秒为单位,或承诺,
  解决时应中止请求。


简单的例子:

  $ provide.factory('myHttpInterceptor',函数($ Q,someService){
  返回{
    请求:功能(配置){        变种消除= $ q.defer();        config.timeout = canceler.promise;        如果属实) {            //取消请求
            canceler.resolve();
        }        返回配置;
    }
  }
});$ httpProvider.interceptors.push('myHttpInterceptor');

Is it possible to prevent a request using angularjs interceptors ?

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

解决方案

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

From the documentation:

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

Simple example:

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

这篇关于AngularJS:如何prevent请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆