在angularfire2 #askfirebase中获取对象的长度

时间:2017-02-17 12:44:31

标签: firebase angularfire2

有没有办法使用angularfire2并获取对象的长度?

1 个答案:

答案 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
        });
    }
}