1
我試圖讓多個http請求後的元素,但有一個異步問題,我無法解決。發佈我的代碼:對象內數組訂購後未定義
Get函數在我的服務:
get() {
return new Observable(project => {
this.channelsService.get().subscribe(
stations => {
this._stations = stations;
this._stations.forEach((station) => {
station.cssClass = this.channelsService.getCss(station.id.split('/').pop());
station.plstation$thumbnails = this.channelsService.getThumbnails(station.id.split('/').pop());
if (station.plstation$callSign !== '') {
const watchliveUrl = this.watchLiveApi + station.plstation$callSign + '/?schema=1.0.0';
this.http.get(watchliveUrl).subscribe(data => {
const body = data.json();
station.currentListing = body.currentListing;
station.nextListing = body.nextListing;
project.next(stations);
project.complete()
});
}
});
}, (error) => {
this.mapErrorService.mapError(error, 'Listing service (1)');
});
});
}
的get()使用和訂閱:
constructor(private listingService: ListingService) {
this.listingService.get().subscribe((stations) => {
this.stripDetails.channelList = stations;
// stations[6].currentListing Not undefined
console.log(stations);
// Now is undefined
console.log(stations[6].currentListing);
}); }
我如何定義站[6] .currentListing ?
我與你的意見修改,但它不工作 – goltornate
的問題是你有一個'forEach'循環發射了多個請求,和你調用'project.complete( )當第一個http請求完成時。您必須將所有請求集中在一起,並在完成所有響應後才完成「項目」。你可以使用'Rx.Observable.forkJoin()'。 – Duncan
我要把這個功能準確的放在哪裏? – goltornate