Angular2: Show placeholder image if img src is not valid

后端 未结 8 1766
迷失自我
迷失自我 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:12

    I Just did this :

    In my HTML FILE wrote this

    I called a function that is in my component.ts and pass the object wheres my url as a parameter

    getImage(result){
    if(result.poster_path){
      return this.imageURL+(result.poster_path);
    }else return "./assets/noFound.jpg"
    

    Heres my function , first I verify if the object image url is different from null if is true then I return the image url else i return my default "noFound" image that is in my app assets.
    Hope it helps!

提交回复
热议问题