【发布时间】:2020-03-06 03:15:42
【问题描述】:
我想在我的列表${this.url}/users?offset=${offset}&limit=12 中初始化 12 个用户,但是随着滚动,这个偏移量应该增加 8 个用户。
我想为此使用无限滚动。我的问题是我正在使用observables(userList),但我不知道如何将 8 个成员的新列表附加到旧列表中。在互联网上的教程中,所有人都使用concat(),但这是针对数组的:/我自己尝试了一些方法,当 loadMore 为真时调用整个列表 + 8 偏移量,但不知何故不起作用。
我的代码:
service.ts
// get 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) //new 8 users
.pipe(map(response => response.results));
}
this.userList = this.userService.getList(this.offset) // initials 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>
【问题讨论】:
-
使用
scan运算符。它作为一个 reducer 工作,但也在每个值发出后发出值(不等待 observable 完成) -
@Sergey 你能把它写到我的代码中作为答案吗?我真的会知道如何应用它,也不知道我写的函数会留下什么。
标签: javascript angular typescript ionic-framework observable