Home > Software design >  How to display image of an API via URL with Angular
How to display image of an API via URL with Angular

Time:04-14

I'm learning Angular and for that I've been developing the Tour of Heroes tutorial from the documentation itself in enter image description here

CodePudding user response:

Based on the screenshot you posted, image is not a string, is an object, change this in your interface:

export interface Heroi {
 id: string,
 name: string,
 image: { url: string }
}
<ul *ngFor="let heroi of herois">  
    <li>
       <p>{{ heroi.id }}</p>
       <p>{{ heroi.name }}</p>
       <img  
        *ngIf="heroi.image?.url"
        [src]="heroi.image.url" 
        [alt]="heroi.name">
    </li>
</ul>
  • Related