分页使用AngularFire2 [英] Pagination using AngularFire2

查看:224
本文介绍了分页使用AngularFire2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



从AngularFire2的文档中,我得到了如下的东西:


$ b $

  const queryObservable = af.database.list('/ items',{
query:{
orderByChild:'size ',
equalTo:subject
}
});

这个效果很好。我有大约300-400条记录,但是我无法在一个API调用中完成。所以我正在尝试实现分页。我尝试了很多东西。我想实现的是这个

  StartAt(),endAt()

我试图通过查询对象,但它没有工作。
AngularFire2是否有这个实现?
如果没有,我怎样才能得到Firebase的ref,这样我就可以编写我自己的实现了?

解决方案

AngularFire2不会根据 startAt 和 endAt blob / master / src / database / firebase_list_factory.tsrel =nofollow>源代码。以下是你如何限制你的查询基于一个列表大小

  let queryObservable = this.af.database.list('/ items',{
query:{
orderByChild:'size',
startAt:50,
endAt:100
}
});

这应该将所有具有 size 定义在50到100之间。

注意我注意到如果我将一个引用对象传入AngularFire2的查询路径,查询将 NOT 工作!所以下面的代码仍然会返回项目列表,但不会被正确排序。

  let ref = firebase.database( ).REF( '/项目'); 
this.af.database(ref,{query:{orderByChild:'size'}});


I am building an Ionic2 project, that uses Firebase and I am using AngularFire2.

From AngularFire2 documentation, I was able to get things like:

const queryObservable = af.database.list('/items', {
  query: {
    orderByChild: 'size',
    equalTo: subject 
  }
});

This works well. I have around 300-400 records, but I can't pull it all in one API call. So I am trying to implement pagination. I tried many things. What I want to implement is this:

StartAt(), endAt()

I tried to pass it through the query object, but it did not work. Does AngularFire2 have an implementation for this? If not, how can I get the Firebase ref so that I can write my own implementation?

解决方案

AngularFire2 does support startAt and endAt according to their source code. Here's how you would limit your query based on a list with a child key of size:

let queryObservable = this.af.database.list('/items', {
  query: {
    orderByChild: 'size',
    startAt: 50,
    endAt: 100
  }
});

This should pull all the items that have a child key of size defined between 50 to 100.

NOTE I have noticed that if I were to pass in a reference object into AngularFire2 instead of a reference path, the query will NOT work! So the code below will still return the list of items but it will not be sorted properly.

let ref = firebase.database().ref('/items');
this.af.database(ref, { query: { orderByChild: 'size' } });

这篇关于分页使用AngularFire2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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