[英]Why Angular.orderBy shows no data in Page?
I am developing a Angular website with help of Firebase Firestore.我正在 Firebase Firestore 的帮助下开发一个 Angular 网站。 It is my first project on Angular.
这是我在 Angular 上的第一个项目。 I have learned Angular 2months ago.
我在 2 个月前学习了 Angular。 Please See the below codes: -
请参阅以下代码: -
Component.html
组件.html
<section class="rank">
<p class="records" *ngIf="members.length === 0">No Records Found.</p>
<div class="text-img" *ngIf="members.length > 0">
<p class="sb">Best Sulphuric</p>
<p class="role">Member</p>
<p class="name">
{{ members[0].payload.doc.data().name }}
</p>
</div>
<table *ngIf="members.length > 0">
<tr>
<th>ID</th>
<th>Name</th>
<th>Posts</th>
<th>Score</th>
</tr>
<tr *ngFor="let member of members; let indexOfelement = index">
<td>{{ indexOfelement + 1 }}</td>
<td>{{ member.payload.doc.data().name }}</td>
<td>{{ member.payload.doc.data().posts }}</td>
<td>{{ member.payload.doc.data().score }}</td>
</tr>
</table>
</section>
Component.ts
组件.ts
import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
@Component({
selector: 'app-rank',
templateUrl: './rank.component.html',
styleUrls: ['./rank.component.scss'],
})
export class RankComponent implements OnInit {
members: any;
constructor(public db: AngularFirestore) {
db.collection('members')
.snapshotChanges()
.subscribe((res) => (this.members = res));
}
ngOnInit(): void {}
}
When I open this on browser this shows all the data in members in Firestore.当我在浏览器上打开它时,它会显示 Firestore 中成员中的所有数据。 But when i change component.ts to this -->
但是当我将 component.ts 更改为此 -->
Component.ts
组件.ts
import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
@Component({
selector: 'app-rank',
templateUrl: './rank.component.html',
styleUrls: ['./rank.component.scss'],
})
export class RankComponent implements OnInit {
members: any;
constructor(public db: AngularFirestore) {
this.members = db.collection('members').ref.orderBy('score');
}
ngOnInit(): void {}
}
It shows no data on window.它显示没有关于 window 的数据。 Can you help me please?
你能帮我吗?
Thanks in Advance for Helping.提前感谢您的帮助。
In the second version,在第二个版本中,
You are missing the你错过了
.snapshotChanges()
.subscribe((res) => (this.members = res));
}
inside the constructor.在构造函数内部。 Without the subscribe, Angular will not make any HTTP Requests and your component will not receive any data.
如果没有订阅,Angular 将不会发出任何 HTTP 请求,并且您的组件将不会收到任何数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.