Angular 如何在角度4中循环观察到的物体

Angular 如何在角度4中循环观察到的物体,angular,loops,rxjs,observable,Angular,Loops,Rxjs,Observable,我有一个分区数组,我试图获取并显示每个分区下的所有项目。这看起来很简单。这是我的密码: //my ngOnInit call this function this.sectionService.GetItemList(this.selectedSectionList, this.culture) .subscribe( (itemList: ItemBySection[]) => { this.itemBy

我有一个分区数组,我试图获取并显示每个分区下的所有项目。这看起来很简单。这是我的密码:

//my ngOnInit call this function 
this.sectionService.GetItemList(this.selectedSectionList, this.culture)       
        .subscribe(
           (itemList: ItemBySection[]) => {
               this.itemBySection = itemList;

               this.loaded = true;
            },
            (error: any) => this.errorMessage = error, () => 
console.log("loaded")
        );

//this is my function in my service
 public GetItemList(SectionItems: Sections[], strCulture): Observable<ItemBySection[]> {

    let resultObservable
    for (var SectionItem in SectionItems) {
        let url = environment.portalWebServiceURL + "api/section/" + SectionItems[SectionItem].sectionItemID.toString() + "/" + strCulture;
        resultObservable = this.http.get(url)
            .catch(this.handleError)
            .mergeMap(res => <ItemBySection[]>res.json());
    }

    return resultObservable; 

}

我希望这能有所帮助。

问题可能是您的http流:res.json()返回解析为json的响应体。 所以应该使用.map()运算符,而不是.mergeMap()运算符

前者简单地将转换函数应用于每个流数据并通过管道传输结果(这就是您所需要的),后者将一个可观察到的所有值投影到您的流中(而这不是您所需要的)

一些参考资料:

  • flatMap(即mergeMap的别名):
  • 地图:
希望有帮助:)

**更新**

好吧,我误解了您的需求:您需要将所有http响应加入一个响应数组中。 你可以这样做:

public GetItemList(SectionItems: Sections[], strCulture): Observable<ItemBySection[]> {
    const resultObservables = []
    for (var SectionItem in SectionItems) {
        let url = environment.portalWebServiceURL + "api/section/" + SectionItems[SectionItem].sectionItemID.toString() + "/" + strCulture;
        const response = this.http.get(url)
            .catch(this.handleError)
            .map(res => <ItemBySection>res.json());
        resultObservables.push(response)
    }
    return Observable.forkJoin(resultObservables); 
}
publicGetItemList(SectionItems:Sections[],结构):可观察{
const resultObservables=[]
用于(SectionItems中的var SectionItem){
设url=environment.portalWebServiceURL+“api/section/”+SectionItems[SectionItem].sectionItemID.toString()+“/”+struculture;
const response=this.http.get(url)
.接住(这个.把手错误)
.map(res=>res.json());
resultObservables.push(响应)
}
返回可观察的forkJoin(resultObservables);
}

我在这里直接编写了这段代码,因此它可能不起作用,但它背后的思想应该是您所需要的:)

如果我使用map,系统只会从第一节中提取项目,但是如果我使用mergeMap,我会出现此错误“找不到不同的支持对象”[对象]'of type'object'。NgFor仅支持绑定到数组之类的Iterables。“我只想显示所有选定节的所有项。这有意义吗?如何将从web服务接收到的所有记录合并或连接到一个可观察项中?好的,现在我不满足您的需要,但您的代码做了一些直接的事情:覆盖ResultoServable变量的每个SectionItems循环。我试图相应地更新我的答案:)太好了,谢谢你,洛伦佐。您的回答将每个调用分隔为数组。我只需要弄清楚如何连接这些数组。我想我应该能弄明白。再次感谢。
public GetItemList(SectionItems: Sections[], strCulture): Observable<ItemBySection[]> {
    const resultObservables = []
    for (var SectionItem in SectionItems) {
        let url = environment.portalWebServiceURL + "api/section/" + SectionItems[SectionItem].sectionItemID.toString() + "/" + strCulture;
        const response = this.http.get(url)
            .catch(this.handleError)
            .map(res => <ItemBySection>res.json());
        resultObservables.push(response)
    }
    return Observable.forkJoin(resultObservables); 
}