IONIC ng用于从firebase恢复数据

时间:2017-06-19 14:09:34

标签: angular ionic-framework ngfor

此代码一直有效,直到在上面的数组控制台上打印的passageiro.ts控制器。

事实是视图循环一个空项。我做错了吗?

(2) [Object, Object]
0 : Object
id : "jMi0U0qT5yO1y04AyLynnQAJEvl1"
nome:"Test1"
__proto__ : Object
1 : Object
id : "v88qudbTpqeDcXWNHpqz0rOzZe92"
nome : "Test2"

服务

getPassageirosList(){
  let passageirosReturn = fb.database().ref('users');
    return new Promise((resolve, reject) => {
      passageirosReturn.orderByChild("tipo_usuario").equalTo("passageiro").on('value', snapshot => {
      let passageiros = [];
      snapshot.forEach( snap => {
      passageiros.push({nome: snap.val().username,id: snap.key});
      return false;
    })
    resolve(passageiros);
  });
});

控制器

ngOnInit(){
    this.passageirosService.getPassageirosList().then( passageiros => {this.passageiros = passageiros; console.log(this.passageiros)});
}

查看

  <ion-list>
    <ion-item *ngFor="let passageiro of passageiros">
      <h2>{‌{pasageiro?.id}}</h2>
    </ion-item>
  </ion-list>

1 个答案:

答案 0 :(得分:0)

在您看来,您可能希望在显示之前检查您是否有passrios列表。 可能不是答案,但尝试在列表中添加* ngIf。

<ion-list *ngIf="passageiros">
  <ion-item *ngFor="let passageiro of passageiros">
    <h2>{‌{pasageiro?.id}}</h2>
  </ion-item>
</ion-list>