In my angular2 application I am retrieving data from Firebase database using query and storing it in a FirebaseListObservable. In my getStatus method I want to find out the number of elements in that FirebaseListObservable. If the list has any elements I want to return 'glyphicon-ok', and if the list is empty I want to return 'glyphicon-remove'. I have shared my code below which is not working.
component.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>
Thanks.