-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的每個循環。 我嘗試相應地更新我的答案:) –
非常感謝洛倫佐。你的回答將每次調用分隔成數組。我只需要弄清楚如何連接這些數組。我想我應該能夠弄清楚。再次感謝。 –