是否可以撤销Firebase列表? [英] Is it possible to reverse a Firebase list?

查看:138
本文介绍了是否可以撤销Firebase列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过关于这个的所有问题,但我仍然没有找到答案。所以不要把它标记为重复的。

我正在使用AngularFire和Angular 2和Typescript。我正在使用 FirebaseListObservable 从端点中提取最近24条记录的列表。这里是我的代码:

 从'@ angular / core'导入{Component}; 
从'angularfire2'导入{AngularFire,FirebaseListObservable};

@Component({
selector:'story-list',
templateUrl:'./story-list.component.html',
styleUrls:[' ./story-list.component.scss']
})
export class StoryListComponent {
stories:FirebaseListObservable< any []> ;;

构造函数(af:AngularFire){
this.stories = af.database.list('/ stories',{
query:{
limitToLast:24
}
});




$ b $ p
$ b $ p $返回24个最新的故事列表,预期,但是当我在页面上显示它们时:

 < p * ngFor =let story of stories | async > {{story.title}}< / p> 

它显示最上面的最旧的故事,以及最下面的最新的故事。我明白这是为什么,而我并不是说这是一个问题,但是我将如何反转呢?我希望能够在查询中反转这一点,但如果这是不可能的,我会打开以某种方式反转它在渲染。



无论你的答案是,请不要只链接到文档。 AngularFire没有文档可以完整地解释这一点。我读过每一个字。请提供实际的工作代码。

然而,我绝对不会接受的一个解决方案是在创建时在记录中存储否定日期,然后根据那。这是我听过的最糟糕的解决方案,真正的数据库系统将允许您在查询中执行此操作。所以请不要提示。



如果您有任何想法,我将不胜感激。我很讨厌必须使用其他产品,因为我喜欢Firebase。

解决方案

据我所知,Firebase查询但是,您可以使用 map 运算符来反转呈现列表的排列顺序反转由 AngularFire2 observable发出的数组:

  importrxjs /添加/操作者/地图; 

...

this.stories = af.database.list('/ stories',{
query:{
limitToLast:24 $ ((array)=> array.reverse())as FirebaseListObservable< any []>;

如果您希望在模板中执行此操作,可以查看这个答案


I've read every question on SO about this and I still haven't found an answer to this. So don't mark this as a duplicate.

I'm using AngularFire with Angular 2 and Typescript. I'm using FirebaseListObservable to pull a list of the 24 most recent records from an endpoint. Here's my code:

import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';

@Component({
  selector: 'story-list',
  templateUrl: './story-list.component.html',
  styleUrls: ['./story-list.component.scss']
})
export class StoryListComponent {
  stories: FirebaseListObservable<any[]>;

  constructor(af: AngularFire) {
    this.stories = af.database.list('/stories', {
      query: {
        limitToLast: 24
      }
    });
  }
}

This returns a list of the 24 latest stories, as expected, but when I render them on the page with:

<p *ngFor="let story of stories | async">{{ story.title }}</p>

It shows the oldest story on the top, and the newest on the bottom. I understand why this is, and I'm not claiming it's an issue, but how would I reverse this? I'd like to be able to reverse this in the query, but if that's not possible, I'd be open to somehow reversing it in the rendering.

Whatever your answer is, please don't just link to documentation. There is no documentation for AngularFire that fully explains this. I've read every word. Please provide real working code.

One solution that I absolutely will not accept, however, is storing a negative date in the records at the time of creation and then sorting based on that. That is the worst solution I've ever heard, and a real database system would allow you to do this in the query. So please don't even suggest it.

If you have any ideas, I would greatly appreciate it. I would hate to have to use another product because I love Firebase.

解决方案

As far as I'm aware, the Firebase queries are always in ascending order.

However, you can reverse the ordering of the rendered list using the map operator to reverse the array that's emitted by the AngularFire2 observable:

import "rxjs/add/operator/map";

...

this.stories = af.database.list('/stories', {
  query: {
    limitToLast: 24
  }
}).map((array) => array.reverse()) as FirebaseListObservable<any[]>;

If you'd prefer to do it in the template, you could have a look at this answer.

这篇关于是否可以撤销Firebase列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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