I'm still pretty new to ionic and angular, and I'm working on a pokedex app. When I try to display images that I pulled in from a JSON file after setting up the data service, it displays the file path instead of the actual image. I'm not sure what I'm doing wrong. Can anyone help?
Here is a snippet from the JSON file:
{
"pocketMonsters": [
{
"pokemonName": "Bulbasaur",
"pokedexNumber": "001",
"description": "Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger",
"pokemonImage": "<img src='assets/imgs/bulbasaur.png' />"
},
{
"pokemonName" : "Ivysaur",
"pokedexNumber" : "002",
"description" : "",
"pokemonImage" : "<img src='assets/imgs/ivysaur.png' />"
},
And here's the template. Besides the code that I already have in here, I have also tried <img src="pocketMonster.pokemonImage />
instead of {{poketMonster.pokemonImage}} and that returned a 404 error in the console.
<ion-header>
<ion-navbar>
<ion-title>
Pokedex
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngFor="let pocketMonster of pocketMonsters;">
{{pocketMonster.pokemonName}}
{{pocketMonster.pokemonImage}}
</ion-item>
</ion-list>
<!--<div *ngIf="pocketMonsters.length"></div> -->
</ion-content>
I'll go ahead and include the home component and the data service in case those are needed as well.
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { pokemonDataService } from '../../providers/data/data';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
pocketMonsters = [];
searchQuery: string = '';
constructor(public navCtrl: NavController, public dataService: pokemonDataService) {
}
ionViewDidLoad() {
this.dataService.getAllPokemon().then((data) => {
data.map((pocketMonster) => {
this.pocketMonsters = data;
return pocketMonster;
});
console.log(this.pocketMonsters);
});
}
//ngOnInit(){
//this.dataService.getAllPokemon()
//.subscribe(data => {
//this.pokemonList = data;
//});
//}
}
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class pokemonDataService {
data: any;
constructor(public http: Http) {
}
getAllPokemon() {
if(this.data){
return Promise.resolve(this.data);
}
return new Promise(resolve => {
this.http.get('assets/data/pokemon.json').map(res => res.json()).subscribe(data => {
this.data = data.pocketMonsters;
resolve(this.data);
});
});
}
}