获取列表 firebase 的列表

Get list of list firebase

我正在使用 Firebase 上的数据库创建 Ionic 应用程序,所以我在 Firebase 中有类似的东西,我正在使用 angularfire2 来获取数据

我获取 paises 没问题,但我尝试获取 jugadores 的列表,但我什么也没得到。

这是我的.ts

export class HomePage {
paises: Observable<any[]>
constructor(public navCtrl: NavController, db: AngularFireDatabase) {
this.paises = db.list('paises').valueChanges();

 }
 }

这是我的 .html

<ion-content *ngFor="let pais of paises | async">
<ion-item-group>
  <ion-item-divider color="light">{{pais.nombre}}</ion-item-divider>
  <ion-item>{{pais.jugadores.Nombre}}</ion-item>
</ion-item-group>

我确实得到了 pais.nombre,但是当我尝试得到 pais.jugadores 时,我得到的只是空白 space。因此,如果有人可以帮助我提供有关如何获取此信息的信息,因为我已经在线搜索但没有任何信息。

我猜你的数据结构是这样的。

[{
    "bandera": "http://someimage.her.png",
    "copas": 0,
    "jugadores": {
        "jugardor0001": {
            "Nombre": "alice",
            "score": 100
        },
        "jugardor0002": {
            "Nombre": "bob",
            "scoe": 80
        }
    }
}, ...]

"paises" 是可以使用 *ngFor 迭代的集合。

<!-- paises > collection, iterable -->
<ion-content *ngFor="let pais of paises | async">    
<ion-item-group>
  <ion-item-divider color="light">{{pais.nombre}}</ion-item-divider>

  <!-- pais.jugadores > not collection, not iterable -->
  <ion-item>{{pais.jugadores.Nombre}}</ion-item>
</ion-item-group>

pais.jugadores 是 不可迭代的对象.

{
    "jugardor0001": {
        "Nombre": "alice"
        "score": 100
    },
    "jugardor0002": {
        "Nombre": "bob"
        "score": 80
    }
}

我们想把上面的object改成collection这样。

[{
    "key": "jugardor0001",
    "value": {
        "Nombre": "alice",
        "score": 100
    }
}, {
    "key": "jugardor0002",
    "value": {
        "Nombre": "bob",
        "scoe": 80
    }
}]

使用"Pipe"将对象数组更改为集合

//pipe.keys.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
    let keys = [];
    for (let key in value) {
      keys.push({key: key, value: value[key]});
    }
    return keys;
  }
}

//app.module.ts
...
import {KeysPipe} from './pipe.keys';
...
@NgModule({
  imports: [
    ... your import
  ],
  declarations: [
    ... your component
    KeysPipe
  ],
....

你的组件会像

<ion-content *ngFor="let pais of paises | async">
<ion-item-group>
  <ion-item-divider color="light">{{pais.nombre}}</ion-item-divider>  

  <!-- using | keys pipe to change object array to collection -->
  <ion-item *ngFor="let jugador of pais.jugadores | keys">
    {{ jugador.key }}: {{ jugador.value.Nombre }}
  </ion-item>
</ion-item-group>

stackblitz 示例是 here .