1
I am building an application that should create a list of Pokemons, but I have the following problems:
My table that should display the Pokemon list does not display anything. When I do the console.log of my array with the result of my method it displays: [Object Object].
The method that should make the list of Pokemons getPokemons does a push on an array called pokemonsList and it is this pokemonList that returns the [Object Object].
I know this [Object Object] is a string, so I tried to make a JSON.stringify to convert to JSON, but it still didn’t work.
What I found interesting is that when I do in Oninit the console.log only the result of my service, it returns the right object.
How can I fix this? Thank you!
HTML
<table class="table">
<thead class="bg-warning">
<tr>
<th scope="col">Nome:</th>
<th scope="col">Url: </th>
</tr>
</thead>
<tbody>
<tr *ngFor="let pokemon of pokemonsList">
<td>{{pokemon.name}}</td>
<td>{{pokemon.url}}</td>
</tr>
<tr>
</tbody>
Listamonscomponent
import { Component, OnInit } from '@angular/core';
import { PokemonModel } from '../../interfaces/pokemon-model'
import { PokemonsService} from '../../services/pokemons.service'
@Component({
selector: 'app-lista-pokemons',
templateUrl: './lista-pokemons.component.html',
styleUrls: ['./lista-pokemons.component.css']
})
export class ListaPokemonsComponent implements OnInit {
pokemon: PokemonModel = new PokemonModel();
pokemonsList: Array <any> = new Array();
constructor(private pokemonService: PokemonsService) { }
ngOnInit(): void {
this.listarPokemons();
this.pokemonService.getPokemons()
.subscribe((response: any) => { console.log(response); } );
}
listarPokemons(){
this.pokemonService.getPokemons()
.subscribe(resultado => {
this.pokemonsList.push(resultado);
JSON.stringify(this.pokemonsList);
console.log('Lista de Pokemons: ' + this.pokemonsList);
} );
}
}
Pokemonsservice
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { PokemonModel } from '../interfaces/pokemon-model';
import { environment } from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class PokemonsService {
constructor(private http: HttpClient) { }
getPokemons(): Observable<any>{
return this.http.get(`${environment.pokemonApiUrl}/pokemon?limit=10`);
}
}