【问题标题】:How to combine parent and child based on parent value using promise in angular?如何使用 Angular 中的 Promise 根据父值组合父值和子值?
【发布时间】:2019-02-12 21:08:12
【问题描述】:

我在使用 httpclient 的 Angular 中使用 Promise 得到响应。但我需要使用父表 id 获取另一个表,并使用 map 或 list 或某种方式将两者结合起来。由于我对这个概念不熟悉,任何人都可以帮助我提供解决方案或建议。

请在下面找到我要获取的代码,我也不确定它是否正确。

  this.parentDetails = this.apiService.getParentDetails().then(
      parents => this.parentsList = parents,
      error => this.errorMessage = <any> error).then(
      parentsList => this.childId = parentsList.childId,
  );

从这里我不知道如何接管并获得两个模型

【问题讨论】:

  • 这对你有用吗?

标签: angular promise


【解决方案1】:
async getData() {
  try {
    // let both start at the same time
    let parentDetails$ = this.apiService.getParentDetails();
    let yourSecondRequest$ = this.apiService.getParentDetails();

    // get the values 
    let parentDetails = await parentDetails$;
    let yourSecondRequest = await yourSecondRequest$;

    parentDetails.something = yourSecondRequest.something;
  } catch (e) {
    // error handling in case a html request fails
  }
}

【讨论】:

    【解决方案2】:

    对于依赖请求,您可以使用 async 和 await。

      async function() 
      {
        var parentrequest1 = await service.getParentDetails().toPromise();
        //do processing of received data
        var childrequest2 = await service.getChildDetatils().toPromise();
        //do processing
      }
    

    【讨论】:

      猜你喜欢
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2023-01-25
      相关资源
      最近更新 更多