问题
I'm trying to do infinite-scroll in a Ionic2+Meteor mobile app which lists the contacts. In the following code, findContacts() function returns 10 contacts at a time. contacts: Observable;
findContacts() :Observable<Contact[]> {
/* logic to pull Contacts is here ...*/
}
this.contactsSub = MeteorObservable.subscribe('contacts', options).subscribe(() => {
MeteorObservable.autorun().subscribe(() => {
if(!this.contacts) {
this.contacts = this.findContacts();
}
});
});
This code gets called every time the user scrolls to the end of the list and gets the next 10 contacts. But the propblem here is, it is not appending to the already listed contacts. It just shows the newly pulled 10 contacts. I tried Observable.concat, but it doesn't give disired results. When i tried mergeMap, it goes it indifnite loop and errors out. Could you please let me know how to concatinate Observable array objects? Any help is greatly appreciated. I'm stuck with this issue for more than a week now. Thanks.
回答1:
You can accomplish this by changing contacts as an Array
contacts: Array<any>= []
findContacts() :Observable<Contact[]> {
/* logic to pull Contacts is here ...*/
}
this.contactsSub = MeteorObservable.subscribe('contacts', options).subscribe(() => {
MeteorObservable.autorun().subscribe(() => {
this.findContacts().subscribe(data => {
this.contacts = this.contacts.concat(data);
});
});
});
来源:https://stackoverflow.com/questions/41929179/combine-rxjs-observable-array