我需要按日期排序的项目,但显然我需要降序排序,以显示正确的顺序.
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中解释的后代顺序。
哪些是我需要的问题?
发布于 2017-06-29 01:21:15
一种方法可能是逆转组件模板中的顺序。首先,您可以在组件中直接得到一个帖子列表:
posts.component.ts
export class PostsComponent {
posts: FirebaseListObservable<any>;
constructor(db: AngularFireDatabase) {
this.posts = db.list('/posts', {
query: {
orderByChild: 'date'
}
});
}
}然后,您可以使用reverse方法在模板中反转发布的顺序:
posts.component.html
<div *ngFor="let post of (posts | async)?.slice().reverse()">
<h1>{{ post.title }}</h1>
</div>发布于 2018-03-16 19:32:07
把这个留给离子3+ AngularFire 4
posts.component.ts
import { AngularFireDatabase } from 'angularfire2/database';
export class PostsComponent {
posts;
constructor(db: AngularFireDatabase) {
this.posts = db.list('/posts', ref => ref.orderByChild('date')).valueChanges();
}
}posts.component.html
<div *ngFor="let post of (posts | async)?.slice().reverse()">
<h1>{{ post.title }}</h1>
</div>发布于 2021-11-18 17:41:25
还应该使用limitTolast()而不是limitToFirst()来确保数组从与DB中筛选器匹配的最后一个值开始(默认情况下,过滤器是递增顺序),然后在数组上使用.reverse来反转顺序:
1-让数组=db.list(‘/posts’)参考=> ref.orderByChild('date').limitToLast(5); 数组= array.reverse();
https://stackoverflow.com/questions/44814165
复制相似问题