combine RxJs Observable array

北城余情 提交于 2019-12-12 02:29:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!