如何使用Angular的$资源取消正在进行的REST调用?
I've been looking over and over for an example on how to cancel an ongoing REST-call using Angular's $resource. I haven't found any solution yet, but from the Angular documentation I got the impression that it should be possible.
我一直在寻找一个关于如何使用Angular的$资源取消正在进行的REST调用的示例。我还没有找到任何解决方案,但从Angular文档中我得到的印象是它应该是可能的。
From the documentation:
从文档:
Usage: $resource(url[, paramDefaults][, actions]);
用法:$ resource(url [,paramDefaults] [,actions]);
One of the actions defined in the documentation:
文档中定义的操作之一:
timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.
timeout - {number | Promise} - 超时(以毫秒为单位),或承诺在解决时应中止请求。
Does anyone have a working example showing how to use this timeout action with promise to cancel an ongoing request? Is it possible?
有没有人有一个工作示例显示如何使用此超时操作和承诺取消正在进行的请求?可能吗?
2 个解决方案
#1
1
Yes this is possible. You have to create a defere and set the promise as parameter:
是的,这是可能的。您必须创建一个defere并将promise设置为参数:
var timeoutPromise = $q.defer();
{timeout: timeoutPromise.promise}
Then you can resolve the promise at any time:
然后您可以随时解决承诺:
timeoutPromise.resolve();
It should also be possible to call $timeout.cancel(timeoutPromise)
. What should be equal to timeoutPromise.reject()
.
也应该可以调用$ timeout.cancel(timeoutPromise)。什么应该等于timeoutPromise.reject()。
$timeout $q
$ timeout $ q
更多相关文章
- [置顶] Python-uiautomator使用说明文档
- 获取错误“ValueError:int()的无效文字,基数为10:'3128;'在运行Tensor
- 如何为Google Cloud Endpoints方法生成pydoc文档?
- python的接口实现zope.interface示例
- Python套接字代理示例,不断收到调用bind()的错误..为什么?
- python过滤html文档中的Tag标签
- Linux内核配置文档
- 《鸟哥的Linux私房菜》读书笔记6——其它帮助/说明文档
- 【转】刚发现一个linux在线文档库。很好很强大。