Goal: Load an image with a dynamic source. If no image is found, then load a placeholder image instead.
This should demonstrate what I\'m trying to
The following approach also works if you want to handle the error in you class:
In your template:
<img [src]='varWithPath' (error) ="onImgError($event)">
In your class:
onImgError(event) {
event.target.src = 'assets/path_to_your_placeholder_image.jpg';
}
<img class="thumbnail-image" src="getImage()" alt="...">
getImage():string{ //I don't know how would you handle your situation here. But you can think of it.
if (this.validImage) // I don't know how would you manage validImage here.
{
return this.validImagePath;
}
return this.placeholderImagePath;
}