Angular2: Show placeholder image if img src is not valid

后端 未结 8 1733
迷失自我
迷失自我 2020-12-12 12:55

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

8条回答
  •  时光说笑
    2020-12-12 13:30

    The following approach also works if you want to handle the error in you class:

    In your template:

    
    

    In your class:

    onImgError(event) { 
        event.target.src = 'assets/path_to_your_placeholder_image.jpg';
    }
    

提交回复
热议问题