首页 > 解决方案 > 在所有 Ajax 请求完成后更新组件变量

问题描述

我有一个 URL 列表,我正在调用每个 URL 的 Ajax 请求,例如

private getDayData(urls: string[]) {
  const sunrise = [];
  from(urls).pipe(
    concatMap(url => ajax(url)),
    map((ajxRes: any) => ({
      date: ajxRes.response.location.time[0].date,
      sunrise: ajxRes.response.location.time[0].sunrise,
      sunset: ajxRes.response.location.time[0].sunset
    }))
  ).subscribe(response => {
    sunrise.push(response);
    console.log(sunrise);
  });
}

在得到所有结果后,我需要返回这个sunrise数组。subscribe每次 Ajax 请求完成时的方法。

标签: angularrxjs

解决方案


推荐阅读