View api with Ionic

Asked

Viewed 75 times

0

Good afternoon to everyone, I am consuming an api, but I can not display it in my template, it is consumed, appears on the console, but I can not in the template.

Component :

import { PaisesProvider } from './../../providers/paises/paises';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  cotacao: string [];
  constructor(public navCtrl: NavController,
              private paisesProvider: PaisesProvider) {


  }

  ionViewDidLoad() {
    this.listaCotacao();  

  }

  listaCotacao() {
    this.paisesProvider.listaPaises()
    .subscribe(data => {
      this.cotacao = data;
      console.log(this.cotacao);

    });
  }
}

Template: 

<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>

    <ion-item  *ngFor="let c of cotacao">
      Data : {{c.date}} Valor Compra {{c.buy}} Valor Venda {{c.sell}}    
    </ion-item>          

</ion-content>

Error appears :

Cannot find a differ supporting Object '[Object Object]' of type 'Object'. Ngfor only Supports Binding to Iterables such as Arrays.

  • Along those lines Data : {{c.date}} Valor Compra {{c.buy}} Valor Venda {{c.sell}} you are saying that in your array there is an object with the buy, sell and date properties. As is the response of console.log(this.cotacao);? Maybe just change your quote array to cotacao: string [{ }]}};

  • This friend : {"Ticker": {"high": "29399.00000000", "low": "28234.00000000", "vol": "126.05166124", "last": "29000.12224000", "buy": "29000.13024000", "sell": "29282.98250000", "date": 1535741070}}

  • Have you noticed that this is a javascript object and not an array?

  • You are using Typescript or Javascript?

  • I noticed yes. I am using Typescript

  • You iterate with ngFor over an array

Show 1 more comment

1 answer

0

You’re giving a *ngFor in some answer that is not an array, that is what it is complaining about, it is coming as an object. If you want to display the fields of that object.

In the case of this Answer there that you reported, it is an object, so you do not need to go through its values, just access them directly as for example: c.date c.buy and so on.

  • Exactly, it’s a super simple array, but I can’t show it.

  • Following api: https://www.mercadobitcoin.net/api/BTC/ticker/ .

  • It doesn’t seem to be a vector but how will I display - la in the template ?

  • In that case you don’t need to give an ngFor, just give a c.ticker.date c.ticker.buy and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.