使用firebase检索angular2中的数据

时间:2016-02-17 22:40:19

标签: firebase angular

我试图用angular2从firebase检索数据今天没有成功,我可以控制日志数据并查看对象,但是为了重用它们我在控制台中未定义,可能是同步和异步数据的问题,我是什么样的解决方案必须用angular2?

检索这些数据

谢谢

  read(){
    this.publicationUrl  = "https://FBURL/publication";
    this.PublicationRef = new Firebase(this.publicationUrl);
    this.PublicationRef.on("child_added", function(snapshot){
      this.publication = snapshot.val();
      console.log(this.publication)

    })
  }

1 个答案:

答案 0 :(得分:3)

问题是您将数据设置为错误的this对象。使用arrow function保留词法范围:

read() {
    this.publicationUrl  = "https://FBURL/publication";
    this.PublicationRef = new Firebase(this.publicationUrl);
    this.PublicationRef.on("child_added", snapshot => {
        this.publication = snapshot.val();
        console.log(this.publication);
    });
}