I use AngularFire2 to get data from Firebase Database (realtime).
What I have done:
- Firebase Database
{ “class” : { “student” : { “Tom” : “male”, “Mary” : “female”, “Peter” : “male”, “Laura” : “female” }, "numberOfStudent” : 10 } }
app.component.ts
import { AngularFireDatabase } from 'angularfire2/database'; import { Observable } from 'rxjs/Observable'; ... export class AppComponent { class: Observable<any>; students: Observable<any[]>; constructor(private db: AngularFireDatabase) { this.class = db.object(‘class’).valueChanges(); this.students = db.list(‘class/student’).snapshotChanges(); } }
app.component.html:
<h2>Class size: {{ (class | async)?.numberOfStudent }}</h2>
<ul>
<li *ngFor="let i of students | async">
{{i.key}} : {{i.value}}
</li>
</ul>
What happened:
Class size: 10
Tom :
Mary :
Peter :
Laura :
It doesn't return the value of list.
Any suggestion is appreciated.
{{ i?.key }} : {{ i?.value }}
, the safe operator?
will stop any errors for the data not being there yet – Polkyconsole.log(this.class, this.students)
– Polky