【发布时间】:2018-01-13 11:11:27
【问题描述】:
我想在我的应用程序中创建一个排行榜。所以我需要从 Firebase 数据库中读取名称和总分信息并将它们显示在浏览器上。 我的目标是在浏览器上显示类似的内容,但当然用数据库用户名和最高分替换未知。
排行榜
第一名:未知,最高分:未知 第二名:未知,最高分:未知 第三名:未知,最高分:未知 第四名:未知,最高分:未知 第五名:未知,最高分:未知
我的 firebase 数据库是:
"users" : {
"Test1" : {
"totalscore" : 50,
"username" : "test1"
},
"Test2" : {
"totalscore" : 30,
"username" : "test2"
},
"Test3" : {
"totalscore" : 20,
"username" : "test1"
},
"Test4" : {
"totalscore" : 10,
"username" : "test1"
},
"Test5" : {
"totalscore" : 50,
"username" : "test1"
},
"Test6" : {
"totalscore" : 30,
"username" : "test2"
},
"Test7" : {
"totalscore" : 20,
"username" : "test1"
},
"Test8" : {
"totalscore" : 10,
"username" : "test1"
}
}
通过下面的代码,我可以得到我想要的反转信息。
topusers: FirebaseListObservable<any>;
constructor(db: AngularFireDatabase) {
this.topusers = db.list('users', {
query: {
orderByChild: "totalscore",
limitToLast: 10,
}
});
}
最后使用 *ngFor 我可以显示从第五名到第一名的 5 个最高分。
<ul *ngFor="let topuser of topusers | async">
<li class = "white" >
{{ topuser.username | json }}:
{{ topuser.totalscore | json }}
</li>
</ul>
我的问题是有什么方法可以控制 *ngFor 所以不是从列表的第一个索引开始从最后一个开始并在第一个结束? 或者无论如何我可以控制我想在浏览器上显示的 FirebaseListObservable 的索引??
【问题讨论】:
-
你可以尝试给observable添加一个map函数来颠倒返回的顺序。 .map(res => res.reverse())
标签: angular typescript firebase angularfire