My goal is to initialize a list with 12 users using the URL
${this.url}/users?offset=${offset}&limit=12
. As users scroll, the offset should increase by 8. I plan to implement infinite scrolling for this purpose. However, I am facing an issue with appending the new list of 8 members to the existing one.
I am currently using observables(userList)
and struggling to find a solution. Most tutorials online recommend using concat()
, but that method is designed for arrays. I tried incorporating the logic to add the list + 8 offset when loadMore is true without success.
Here's a snippet of my code:
service.ts
// Fetch a list of users
getList(offset = 0): Observable<any> {
return this.http.get(`${this.url}/users?offset=${offset}&limit=12`);
}
page.ts
@ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
userList: Observable<any>;
offset = 0;
...
getAllUsers(loadMore = false, event?) {
if (loadMore) {
this.userList = this.userService.getList(this.offset += 8) // Get new 8 users
.pipe(map(response => response.results));
}
this.userList = this.userService.getList(this.offset) // Initialize 12 users
.pipe(map(response => response.results));
if (event) {
event.target.complete();
console.log(event);
console.log(loadMore);
}
}
page.html
...
</ion-item>
</ion-list>
<ion-infinite-scroll threshold="100px" (ionInfinite)="getAllUsers(true, $event)">
<ion-infinite-scroll-content
loadingSpinner="crescing"
loadingText="Loading more data...">
</ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-slide>
<ion-slide>