【发布时间】:2017-11-29 08:47:38
【问题描述】:
在我的 angular2 应用程序中,我使用查询从 Firebase 数据库中检索数据并将其存储在 FirebaseListObservable 中。在我的 getStatus 方法中,我想找出 FirebaseListObservable 中的元素数量。如果列表有任何元素,我想返回“glyphicon-ok”,如果列表为空,我想返回“glyphicon-remove”。我在下面分享了我的代码,但它不起作用。
组件.ts
assignments: FirebaseListObservable<any>
submission: FirebaseListObservable<any>
ngOnInit() {
this.assignments = this._getAsnService.getAsnByCourseBatch(AuthService.courseBatch);
}
getStatus(asnDetailKey) {
// Searching assignment in database
this.submission = this._db.list(`/submissions/${AuthService.uid}/`, {
query: {
orderByChild: 'asnDetailKey',
equalTo: asnDetailKey
}
});
// If assignment is found return 'glyphicon-ok' else return 'glyphicon-remove'
this.submission.subscribe(sub => {
this.status = sub.length > 0 ? 'glyphicon-ok' : 'glyphicon-remove';
});
return this.status;
}
component.html
<table class="table table-bordered" *ngIf="!isLoading">
<tr>
<th>Name</th>
<th>Subject</th>
<th>Due Date</th>
<th>Status</th>
</tr>
<tr *ngFor="let assignment of assignments | async" [hidden]="filter(assignment)">
<td> <span class="AsnName" [routerLink]="['view', assignment.$key]"> {{ assignment.AsnName }} </span> </td>
<td> {{ assignment.subject }} </td>
<td> {{ assignment.dueDate }} </td>
<td> <i class="glyphicon" [ngClass]="getStatus(assignment.$key)"></i> </td> // <==== calling getStatus(assignment.$key)
</tr>
</table>
谢谢。
【问题讨论】:
标签: angular typescript firebase firebase-realtime-database observable