结合RxJs Observable数组 [英] combine RxJs Observable array

查看:248
本文介绍了结合RxJs Observable数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在列出联系人的Ionic2 + Meteor移动应用程序中进行无限滚动。在以下代码中,findContacts()函数一次返回10个联系人。
联系人:Observable;

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();
        }
      });
  });

每次用户滚动到列表末尾并获得下一个10个联系人时,都会调用此代码。但这里的问题是,它不会附加到已经列出的联系人身上。它只显示了新拉出的10个联系人。我尝试过Observable.concat,但它没有给出令人反感的结果。当我尝试使用mergeMap时,它就会出现无关紧要的循环和错误。
你能告诉我如何连接Observable数组对象吗?任何帮助是极大的赞赏。我已经坚持这个问题一个多星期了。
谢谢。

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.

推荐答案

您可以通过将联系人更改为数组来实现此目的

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);
        });
      });
  });

这篇关于结合RxJs Observable数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆