【问题标题】:Calling GET Web API call after DELETE Web API has finished in Angular在 Angular 中完成 DELETE Web API 后调用 GET Web API 调用
【发布时间】:2022-11-22 02:46:47
【问题描述】:

这是我的代码:

if (confirm("Are you sure you want to delete the contact?")) {
    this.http.delete(this.apiRoot + id)
        .subscribe()

    this.loadContacts()
        .subscribe(); 
}                           

尝试了很多东西,但基本上我希望 Web API 方法在调用 loadContacts() 之前返回

【问题讨论】:

    标签: angular asp.net-web-api


    【解决方案1】:

    如果您想“等待”直到删除请求完成,您可以使用 switchMap RxJS 运算符。

    this.http.delete(this.apiRoot + id).pipe(
        switchMap(() => {
            return this.loadContacts();
        })
    ).subscribe();
    

    当外部对象发出值时,这将切换到内部可观察对象。更多信息可以found on the Learn RxJS网站。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-03
      • 2021-01-10
      • 2023-02-21
      • 2020-09-06
      • 2014-05-19
      • 2015-05-18
      • 2016-01-08
      • 2012-10-22
      相关资源
      最近更新 更多