Boa Tarde pessoal, estou consumindo uma api com Angular, porem quando executo no browser, aparece esse erro :
Cannot find a differ supporting object ‘[object Object]’ of type ‘object’. NgFor only supports binding to Iterables such as Arrays.
Meu template :
-
{{p.url}} {{p.name}}
Component .ts :
import { HttpClient } from ‘@angular/common/http’;
import { ApiService } from ‘./…/api.service’;
import { Component, OnInit } from ‘@angular/core’;
@Component({
selector: ‘app-poke-listagem’,
templateUrl: ‘./poke-listagem.component.html’,
styleUrls: [’./poke-listagem.component.css’]
})
export class PokeListagemComponent implements OnInit {
pokemon: Array;
constructor(private apiService: ApiService) { }
ngOnInit() {
this.listar();
}
listar() {
this.apiService.listar()
.subscribe(dados => this.pokemon = dados);
}
}
Service.ts:
import { Injectable } from ‘@angular/core’;
import { HttpClient } from ‘@angular/common/http’;
@Injectable({
providedIn: ‘root’
})
export class ApiService {
PokeUrl=‘http://pokeapi.co/api/v2/pokemon/’; //pokemon
constructor(private http: HttpClient) { }
listar() { //lista os pokemon
return this.http.get<any[]>(${this.PokeUrl}
);
}
}