Angular2:同步两个post请求

Angular2: Synching two post requests

本文关键字:两个 post 请求 同步 Angular2      更新时间:2023-09-26

我有两个ajax post请求,需要作为同步调用执行。场景是表单正在创建两种不同类型的对象,这些对象需要作为post请求发送到不同的API,并且我必须在UI上显示动画,直到两个API返回结果到UI。

任何帮助!

编辑添加代码

  doApproveSingle() {
    //animation part
    this.loading = true
    //call 1
    this._http.post('api/single', this.approvedChanges, contentHeaders).subscribe(resp => { 
    }, error => {
    })
    //call 2
    this._http.post('api/new', this.approveNew, contentHeaders).subscribe(resp => {
    }, error => {
    })
  }

有两个post请求,我需要在两个调用完成后关闭动画,需要在那部分的帮助

  doApproveSingle() {
    //animation part
    this.loading = true
    //call 1
    let obs1 = this._http.post('api/single', this.approvedChanges, contentHeaders)
    .map(resp => { ... });
    //call 2
    let obs2 = this._http.post('api/new', this.approveNew, contentHeaders)
    .map(resp => { ... });
    Observable.zip([obs1, obs2]).subscribe(val => this.loading = false);
  }

如果在各个HTTP调用完成后没有什么可做的,则可以省略.map(...)部分。