doorman
doorman

Reputation: 16959

Default image for img element

I am using the img element like this with a pipe

<img [src]="filename | pathPipe" />

I would like to specify a default image which is used if the image path is broken. Can this be accomplished inside the template e.g. with a [default] property?

Upvotes: 0

Views: 2894

Answers (2)

ランス
ランス

Reputation: 540

Inside the pathPipe if you have something like an async call or downloading an image. You can set a default value using startsWith inside a pipe func.

Upvotes: 0

j2L4e
j2L4e

Reputation: 7050

The img element provides an error event, so this should work fine:

<img [src]="filename | pathPipe" (error)="filename = '/path/to/fallback.jpg'" />

Upvotes: 3

Related Questions