Angular 如何在继续之前等待for循环中的订阅完成

Angular 如何在继续之前等待for循环中的订阅完成,angular,typescript,observable,Angular,Typescript,Observable,我在for循环中有一个订阅,它从外部源获取JSON数据作为一系列“类别数据”,然后根据用户当前位置过滤该数据。我需要的是等待所有订阅完成,然后我的应用程序才能继续。现在,它并不等待所有订阅完成,它只完成少数订阅,然后继续,而其他订阅在后台继续 我已经尝试了下面的“暴力”方法,我知道一定数量的类别将被添加到过滤数组中,它是有效的,但我不知道如何使它在任何情况下都有效 这是我的密码: getMultipleCategoryData(categoryIds: string[]) { for (let

我在for循环中有一个订阅,它从外部源获取JSON数据作为一系列“类别数据”,然后根据用户当前位置过滤该数据。我需要的是等待所有订阅完成,然后我的应用程序才能继续。现在,它并不等待所有订阅完成,它只完成少数订阅,然后继续,而其他订阅在后台继续

我已经尝试了下面的“暴力”方法,我知道一定数量的类别将被添加到过滤数组中,它是有效的,但我不知道如何使它在任何情况下都有效

这是我的密码:

getMultipleCategoryData(categoryIds: string[]) {
for (let i = 0; i < this.waypointIds.length; i++) {
//after user selects their categories, its added to waypointNames, and occurences() gets the # of occurences of each waypoint name
  let occurences = this.occurences(this.waypointNames[i], this.waypointNames);
  this.categoryApi.getCategoryData(this.waypointIds[i]).toPromise().then(data => {

    let filteredLocs = data.locations.filter(loc => this.distanceTo(loc, this.hbLoc) < MAX_RADIUS);
    let foundLocs = [];
    //fill filteredLocs with first n, n = occurences, entries of data 
    for (let n = 0; n < occurences; n++) {
      if (filteredLocs[n] != undefined) {
        foundLocs[n] = filteredLocs[n];
      }
    }
    //find locations closest to hbLoc (users current location), and add them to waypoints array
    for (let j = 0; j < foundLocs.length; j++) {
      for (let k = 0; k < filteredLocs.length; k++) {
        if (this.distanceTo(this.hbLoc, filteredLocs[k]) < this.distanceTo(this.hbLoc, foundLocs[j]) && foundLocs.indexOf(filteredLocs[k]) < 0) {
          foundLocs[j] = filteredLocs[k];
        }
      }
    }
    if (foundLocs.length > 0 && foundLocs.indexOf(undefined) < 0) {
      for (let m = 0; m < foundLocs.length; m++) {
        this.waypointLocs.push(foundLocs[m]);
      }
    }
  }).then(() => { 
    //this hardcoded, brute force method works, but i'd need it to be more elegant and dynamic
    if (this.waypointLocs.length >= 5) {
      let params = { waypointLocs: this.waypointLocs, hbLoc: this.hbLoc };
      this.navCtrl.push(MapPage, params);
    }
  });
}
}
getMultipleCategoryData(CategoryId:string[]){
for(设i=0;i{
设filteredLocs=data.locations.filter(loc=>this.distanceTo(loc,this.hbLoc)0&&foundLocs.indexOf(未定义)<0){
对于(设m=0;m{
//这种硬编码的蛮力方法很有效,但我需要它更优雅、更动态
如果(this.waypointLocs.length>=5){
设params={waypointLocs:this.waypointLocs,hbLoc:this.hbLoc};
this.navCtrl.push(映射页面,参数);
}
});
}
}
以及categoryApi.getCategoryData方法:

getCategoryData(categoryId): Observable<any> {
    // don't have data yet
    return this.http.get(`${this.baseUrl}/category-data/${categoryId}.json`)
        .map(response => {
            this.categoryData[categoryId] = response.json();
            this.currentCategory = this.categoryData[categoryId];
            return this.currentCategory;
        });
}
getCategoryData(categoryId):可观察{
//还没有数据
返回this.http.get(`${this.baseUrl}/category data/${categoryId}.json`)
.map(响应=>{
this.categoryData[categoryId]=response.json();
this.currentCategory=this.categoryData[categoryId];
返回此.currentCategory;
});
}

除了等待订阅完成之外,一切都很正常,我真的很想知道一种确定所有订阅何时完成的方法。感谢您的帮助

您可以收集阵列中的所有观测值,并使用以等待所有观测值完成:

let observables: Observable[] = [];
for (let i = 0; i < this.waypointIds.length; i++) {
    observables.push(this.categoryApi.getCategoryData(this.waypointIds[i]))
}
Observable.forkJoin(observables)
    .subscribe(dataArray => {
        // All observables in `observables` array have resolved and `dataArray` is an array of result of each observable
    });
let Observable:Observable[]=[];
for(设i=0;i{
//“observables”数组中的所有可观测值都已解析,“dataArray”是每个可观测值的结果数组
});

伟大而简单的解决方案!工作得很有魅力。非常感谢。