I have showing youtube videos by using async pipe in Angular 6. It's working fine for the first load.
This is how am using it.
Component HTML file:
<div id="youtube" class="search-results" *ngIf="(trendingVideos | async) as videos; else loadingOrError"></div>
Component TS file:
public trendingVideos: Observable<VideoClass[]>;
private loadVideos(videosPerPage?: number, regionCode?: string, videoCategoryId?: number) {
this.trendingVideos = this.youtubeService.getTrendingVideos(videosPerPage, regionCode, videoCategoryId, false)
.pipe(
catchError((error: any) => {
this.loadingError$.next(true);
return throwError(error);
})
);
}
Catch error is working fine also.
Service File:
public getTrendingVideos(videosPerPage?: number, regionCode?: string, videoCategoryId?: number, paginationMode?: boolean): Observable<VideoClass[]> {
const params: any = {
part: appConfig.partsToLoad,
chart: appConfig.chart,
videoCategoryId: videoCategoryId ? videoCategoryId : appConfig.defaultCategoryId,
regionCode: regionCode ? regionCode : appConfig.defaultRegion,
maxResults: videosPerPage ? videosPerPage : appConfig.maxVideosToLoad,
key: appConfig.youtubeApiKey
};
return this.http.get<any>(appConfig.getYoutubeEndPoint('videos'), { params })
.pipe(
map(
(data) => {
return data.items
.map((item) => new VideoClass(item))
.filter((item) => item.id !== '');
}
),
catchError(this.handleError('getTrendingVideos'))
) as Observable<VideoClass[]>;
}
It's working fine when I load data the first time. Now I am developing infinite scroll. So I am calling this API again. But want to merge data into previously loaded data.
It replaces the data every time with new data when my infinite scroll plugin calls this.
This is infinite scroll function:
private onScroll(){
let videosData:Observable<VideoClass[]> = this.youtubeService.getTrendingVideos(this.videoCount, this.regionCode, this.categoryID, true)
.pipe(
catchError((error: any) => {
this.loadingError$.next(true);
return throwError(error);
})
);
// Here how i merge this same video data to previously loaded trending videos data.
}
Any Help?
arrayof VideoClass and append the result of api in that array. Use the samearrayto display data in html.*ngIfto check if length ofarrayis0. If its0, it means the data is yet to be retrieved from api. iflength > 0, you can display the data and on further scrolling, you can call your api and append the data in same array.