我試圖在點擊加載更多後更新Observable數組,但會導致多次調用API。如何在加載時更新可觀察數組更多
這裏是我的html:
<app-productlistother *ngFor="let item of favs$ | async" [item]="item"></app-productlistother>
<a (click)="loadMore()">Load More</a>
組件:
ngOnInit() {
this.favs$ = this._favoriteService.getFavorites(this.pageNum);
}
private loadMore() {
this.pageNum = this.pageNum + 1;
this.favs$ = this.favs$.merge(this._favoriteService.getFavorites(this.pageNum));
}
和服務:
getFavorites(page): Observable<Response> {
return this._http.get(environment.baseUrl + 'favorite?page=' + page)
.map((result: Response) => result.json().data);
}
我怎樣才能解決這個問題?在每個新的請求getFavorites(page)
我需要在陣列的底部推新結果...
您需要在您的服務中使用observable,並且需要訂閱組件中的obeservable,並且一旦加載更多的數據就會被觸發,數據將被添加到observable中,並且組件將會列出所有更改,然後自行更新 –
您能給我舉個例子嗎? – Vladimir
@Vladmir舉了一個例子,希望它有幫助 –