-2
我有一个部分数组,我试图获取并显示每个部分下的所有项目。这似乎是一个非常简单的要求。这里是我的代码:如何在Angular中循环观察值4
//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;
}
也许我上面的解释是不是太清楚 那么我现在要做的是调用一个循环中我的web服务的多个时间和结果连接成一个列表。也许这会有所帮助。
for all my sectionIDs {
call url web service with sectionID
receive results from server
add the results in my item array
}
finally display all items.
我希望这可以帮到你。
如果我使用映射系统只从第一部分拿起项目,但是如果我使用mergeMap,我得到这个错误“无法找到'object'类型的不同支持对象'[object Object]',NgFor只支持绑定到Iterables,比如数组。我只想显示所有选定部分的所有项目。那有意义吗?我如何将从Web服务接收到的所有记录合并或连接成1个可观察对象? –
好吧,现在我不明白你的需求,但你的代码做了一些dirrefent:你正在覆盖resultObservable变量的SectionItems的每个循环。 我尝试相应地更新我的答案:) –
非常感谢洛伦佐。你的回答将每次调用分隔成数组。我只需要弄清楚如何连接这些数组。我想我应该能够弄清楚。再次感谢。 –