有没有办法使用angularfire2并获取对象的长度?
答案 0 :(得分:2)
试试这个:
import { FirebaseApp } from "angularfire2";
import { Inject } from "@angular/core";
export class AppComponent {
constructor(@Inject(FirebaseApp) fb: any) {
const ref = fb.database().ref();
ref.child('/lists')
.once('value')
.then(
(snapshot) => {
console.log(snapshot.numChildren()); // gets length
}
);
}
}
或
import { AngularFireDatabase } from "angularfire2";
export class AppComponent {
constructor(private afd: AngularFireDatabase) {
const lists = afd.object(`/lists`, { preserveSnapshot: true });
lists.subscribe(snapshot => {
console.log(snapshot.numChildren()); // gets length
});
}
}