如何取消 AngularJS 中的 $http 请求? [英] How to cancel an $http request in AngularJS?

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

问题描述

在 AngularJS 中给定一个 Ajax 请求

Given a Ajax request in AngularJS

$http.get("/backend/").success(callback);

如果启动另一个请求(例如相同的后端,不同的参数),取消该请求的最有效方法是什么?

what is the most effective way to cancel that request if another request is launched (same backend, different parameters for instance).

推荐答案

此功能被添加到1.1.5 发布通过超时参数:

This feature was added to the 1.1.5 release via a timeout parameter:

var canceler = $q.defer();
$http.get('/someUrl', {timeout: canceler.promise}).success(successCallback);
// later...
canceler.resolve();  // Aborts the $http request if it isn't finished.

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

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