如何使用AngularFire2 ListObservable从对象获取单个值

时间:2016-11-20 23:19:36

标签: angular angularfire2

当我打电话给Firebase时,我会回到快照,如何在SnapShot中访问单个值?这就是我所拥有的:

topics: FirebaseListObservable<any[]>;

constructor(af: AngularFire) { 
    this.topics = af.database.list('/topics',{preserveSnapshot: true});
    this.topics.subscribe(snapshots => {
    snapshots.forEach(snapshot => {
      console.log(snapshot.key)
      console.log(snapshot.val())
    });
  });

该代码将打印到控制台:

   Object {desc: "Answer questions about music!", imgURL: "seta.png", lang: "eng", published: true, title: "Music"}

让我说我只想从那里获得头衔,我该怎么做?

1 个答案:

答案 0 :(得分:0)

从Firebase查看Javascript文档我想出了一个解决方案,基本上所有我必须在这个值之前调用Snapshot子:

snapshot.child("title").val()