Angular 2 - Check if image url is valid or broken

后端 未结 9 1995
感情败类
感情败类 2020-11-29 19:31

I am fetching a large number of image URLs from an API and display them in a angular 2 web application. Some of the URLs are broken and i want to replace them with a default

9条回答
  •  情歌与酒
    2020-11-29 20:17

    If you want to change an image that is not loading or the source is broken... Angular 8, just have to change the src of this target for a default asset:

    • Component HTML

      <... [src]="person.pictureUrl" (error)="pictNotLoading($event)" >

    • Component TS

      pictNotLoading(event) { event.target.src = 'assets/nopicture.png'; }

提交回复
热议问题