I want to use a default image as a placeholder if an actual image is failed to load or large image is taking much time in a website.

so, I can show default image in case of angular2 image onerror

Bhaskar Monitor Asked on March 25, 2018 in Programming.
Add Comment
  • 2 Answer(s)

    Here are details if we have to set a placeholder image as default image in case of an image is failed to load for angular 2.

    Angular 2 OnError to set a default image if link is broken or not valid

    Need to handle a OnError event. It can be fired automatically when external image or document is failed to load.

    Angular 2 OnError event is same as javascript error event.
    This event can be called two ways.
    a) call a function to handle error event in angular 2 script

    For example.

    
    <img onError="default_image()" src="htps://images.pexels.com/photos/230/landscape-nature-forest-trees.jpg" alt="" height="250px" /> 
    //component:
    default_image() {
     this.IMAGE_PATH =  "DEFAULT_IMAGE_PATH";
     }
    

    b) write expression statement to handle error event

    no need to call function, default image can be set within a statement in angular 2 scripts.

    Syntax:

    
     <img src="IMG_PATH"  onError="this.src='DEFAULT_IMG'" alt="" height="250px" />
     

    Example:
    
     <img onError="this.src='http://via.placeholder.com/375x250/009688/000000?text=Default%20Image'" src="htps://images.pexels.com/photos/230/landscape-nature-forest-trees.jpg" alt="" height="250px" />
     

    Demo set a default image in angular2

    Bhaskar Monitor Answered on March 26, 2018.
    Add Comment

    Hi! I need your opinion. I recently created my own website. I want to earn extra income, so I decided to create a website for playing online casinos. Here is my site https://gmblsites.com/spin-palace . How much you like my site and what disadvantages it has. It is important for me to get feedback.

    Karolina90 Default Answered on August 26, 2020.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.