angular - 无限调用API方法(检查状态)
问题描述
有没有一种方法可以无限地调用Angular(或Ionic)中的函数而不会停止?
我正在模拟付款过程。流程如下:我使用 POST 方法发送付款,API 将我发送到提供付款状态的 URL。
在我这边,我必须等到我从后面得到这个付款状态。所以我有一个检查状态的方法。我想无限地调用这个方法,直到我得到状态。
我看到 Angular 中有“ng-repeat”,但我没有处理 HTML 模板,它在我的 TypeScript 文件中。
这是我的代码:
ionViewWillEnter(){
this.paymentService.postPayment(id, total, currency).subscribe({
next: () => {
// Here I want to check status undefinitely
this.paymentService.checkStatus().subscribe({
next: (result) => {
// WHEN RESULT IS SUCCESS, DO REDIRECTION...
this.router.navigate(['...']);
},
error: () => {
},
});
}
}
解决方案
您可能想了解更多关于 RxJs 和 HTTP 请求的信息。
主要思想是使用 Angular 创建一个可观察的 http 请求HttpClient
,然后.pipe()
使用 RxJs 运算符使其在失败时重试。
这是 Angular 文档中的一个示例:
getConfig() {
return this.http.get<Config>(this.configUrl)
.pipe(
retry(3), // retry a failed request up to 3 times
catchError(this.handleError) // then handle the error
);
}
我猜你必须修改它并应用一些运算符来告诉 RxJs 你如何为你的请求定义失败/成功,但没有更多细节就不容易说。
推荐阅读
- javascript - 如何将传单绑定到 JavaScript 中的坐标列表
- bash - 将数组改造成两个新的 var
- c++ - 如何在 Arduino 中使用 C++ 成员函数作为中断处理程序?
- php - 如何在JS中获取数组输入文本的最后一个值?
- python - 我有几个 Linux 命令,我想在 Windows 机器上运行。如何在 Windows 上从 Python 运行 Linux 命令
- reactjs - Docker-compose - node_modules 未在主机上填充
- java - Java中的线程安全访问枚举
- wordpress - 如何使用 wp 火箭缓存插件停止缓存特定 div?
- c# - dotnet core-类库中的日志记录
- html - :hover 有效,但其余链接状态无效(例如:link & :visited)