我试图用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)
})
}
答案 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);
});
}