Problem with the Observables

0

I have a problem, when presenting the data in the view with ionic 2.

** This is the function of my service **

 getUsers():Observable<any>{
    return this.http.get('https://jsonplaceholder.typicode.com/posts').map((datos) => datos.json());
  }

This is my view, start.ts

 respuesta = [];
  ionViewDidLoad() {
    this.platform.ready().then(() => {
        this.apiServiceProvider.getUsers().subscribe((res) =>{
        this.respuesta.push(res);
        console.log(res);
      });

This is from my view home.html

   <ion-card *ngIf="!respuesta">
     <ul id="todo-list">
          <li *ngFor="let item of respuesta" >
            {{ item.body }}
          </li>
      </ul>
    </ion-card>

In itself, if I print the data by console.

Something missing? What is going wrong? Thanks.

    
asked by JuanL 21.02.2018 в 18:01
source

1 answer

1

If you do push you are uploading to the index respuesta[0] the result of your JSON ... there are two options:

How you do it

 <ion-card *ngIf="!respuesta">
 <ul id="todo-list">
      <li *ngFor="let item of respuesta[0]" >
        {{ item.body }}
      </li>
  </ul>
</ion-card>

As it should be:

this.respuesta = res;

And in the html

<ion-card *ngIf="!respuesta">
 <ul id="todo-list">
      <li *ngFor="let item of respuesta" >
        {{ item.body }}
      </li>
  </ul>
</ion-card>
    
answered by 22.02.2018 / 20:08
source