firebase 和 Ionic 2 上的降序 orderByChild() [英] Descending orderByChild() on firebase and Ionic 2

查看:18
本文介绍了firebase 和 Ionic 2 上的降序 orderByChild()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按日期排序的项目,但显然我需要降序排序以正确顺序显示帖子...

I need get items sorted by date, but obviously I need descending sorted to show the posts in correct order...

import {
  AngularFireDatabase
} from 'angularfire2/database';
import 'rxjs/add/operator/map';

/*
  Generated class for the FirebaseProvider provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class FirebaseProvider {

  constructor(public afd: AngularFireDatabase) {}


  getPostsItems() {
    return this.afd.list('/Posts/', {
      query: {
        orderByChild: "date",
      }
    });

  }

此查询返回升序,我需要 Firebase 网络中未解释的降序.

This query returns a ascendent order and I need a descendent order that it's not explained in Firebase web.

我需要哪些查询?

推荐答案

一种方法是颠倒组件模板中的顺序.首先,您可以直接在组件中获得帖子列表:

One approach could be reversing the order in your component's template. First, you get a list of posts directly in your component:

posts.component.ts

    export class PostsComponent {
      posts: FirebaseListObservable<any>;
     
      constructor(db: AngularFireDatabase) {
        this.posts = db.list('/posts', {
          query: {
            orderByChild: 'date'
          }
        });
      }
    }

然后,您可以使用 reverse 方法来反转您的帖子在模板中的顺序:

Then, you can use the reverse method to reverse your posts' order in your template:

posts.component.html

    <div *ngFor="let post of (posts | async)?.slice().reverse()">
      <h1>{{ post.title }}</h1>
    </div>

这篇关于firebase 和 Ionic 2 上的降序 orderByChild()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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