I am creating Ionic app with database on Firebase,so I have something like this in Firebase, and I am using angularfire2
to get the data
and I have no problem on getting paises
but I have tried getting the list of jugadores
but I get nothing.
This is my .ts
export class HomePage {
paises: Observable<any[]>
constructor(public navCtrl: NavController, db: AngularFireDatabase) {
this.paises = db.list('paises').valueChanges();
}
}
and this is my .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>
I do get the pais.nombre
but when i try to get pais.jugadores
all i get is blank space. So if anybody could help me giving me info on how to get this tipe of information, because I have search online and nothing.
I presume your data structure like this.
[{
"bandera": "http://someimage.her.png",
"copas": 0,
"jugadores": {
"jugardor0001": {
"Nombre": "alice",
"score": 100
},
"jugardor0002": {
"Nombre": "bob",
"scoe": 80
}
}
}, ...]
"paises" is collection which is iterable using *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 is object which is not iterable.
{
"jugardor0001": {
"Nombre": "alice"
"score": 100
},
"jugardor0002": {
"Nombre": "bob"
"score": 80
}
}
We want to change above object to collection like this.
[{
"key": "jugardor0001",
"value": {
"Nombre": "alice",
"score": 100
}
}, {
"key": "jugardor0002",
"value": {
"Nombre": "bob",
"scoe": 80
}
}]
use "Pipe" to change object array to collection
//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
],
....
your component will be like
<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 example is here .