I have the following code:
View:
<ion-card *ngFor="let result of results">
<ion-card-header>
<h2>{{result.nome}}</h2>
</ion-card-header>
<ion-card-content>
<h2>{{result.descrizione}}</h2>
</ion-card-content>
</ion-card>
Controller:
export class HomePage {
results: any;
constructor(
public event: Evento) {
this.results = this.event.EstraiEventi();
}
}
Model:
export class Evento {
EstraiEventi() {
let lista;
this.http.get("http://localhost:53965/api/eventi/estraieventi", '')
.subscribe(
data => {
lista = JSON.parse(data["_body"]);
},
error => {
}
);
return lista;
}
}
the object lista is not filled up because “return lista;” is executed before “lista = JSON.parse(data[”_body"]);" (since http.get function is asynchronous), so my view is empty.
How can I do this thing in a synchronous way?