83

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 picture that is stored locally on my webserver. Does anyone have a suggestion how to test the urls and in the case of status code 404 replace the broken image?

Thanks!

11 Answers 11

172

Listen to the error event of the image element:

<img [src]="someUrl" (error)="updateUrl($event)">

where updateUrl(event) { ... } assigns a new value to this.someUrl.

Plunker example

If you want to check in code only you can use the method explained in Checking if image does exists using javascript

@Directive({
  selector: 'img[default]',
  host: {
    '(error)':'updateUrl()',
    '[src]':'src'
   }
})
class DefaultImage {
  @Input() src:string;
  @Input() default:string;

  updateUrl() {
    this.src = this.default;
  }
}

Directive Plunker example

Sign up to request clarification or add additional context in comments.

9 Comments

Why is this down voted? AFAIK this is the correct method, leveraging the browsers events...
@GünterZöchbauer would it be good to have directive in place to handle such thing..? any thoughts? +1
You're right. I somehow missed the "default picture" and assumed some calculation to get an alternative URL.
Thank you, it just solved a IIS relative path problem with images for me. U da boss!
/* Angular 11 */ @Directive({ selector: 'img[default]', }) export class DefaultImageDirective { constructor(private elem: ElementRef) { } @Input() default = 'assets/placeholder.png'; @HostListener('error') onError(): void { this.elem.nativeElement.src = this.default; } }
|
56

You can use onError event this way to handle invalid url or broken url.

<img [src]="invalidPath" onError="this.src='images/angular.png'"/> 

This way you can directly assign img path to src with onError event

10 Comments

check the plunker at least. Its I think not about being correct or not correct . It about having some other ways....what you say?
No. I'm not taking it wrong way dont worry. Just saying there can be some other options too. This is one of them. This way you can directly assign path.
Thanks @PankajParkar
It's a little off topic...can anybody explain the difference between (error) and onError? They are not the same.
Works fine on Angular 6
|
28

My example on angular 4

<img [src]="img" (error)="img.src = errorImg" #img>
  1. Where img - path to image;
  2. error - error emmit
  3. errorImg - path to default img
  4. #img - link to img object

2 Comments

its the cleanest way to do it, works without any issue
How do you make the #img link (as well as the img reference in the error handler) dynamic in the case where you have the image tag in an ngFor?
8

I use a base64 loading spinner:

<img [src]="photoContainer.photo.url | secure" onError="this.src='data:image/svg+xml;base64,PD94bWw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TA2MUMyNi45MzQyLDkuOTQ2MzggMjYuOTM0Miw3LjQxNzU4IDI4LjQ5MzksNS44NTc4NloiIHN0eWxlPSJmaWxsOnJnYig1MCw1MCw1MCk7Ii8+CiAgICAgICAgPC9nPgogICAgPC9nPgo8L3N2Zz4K'">

Comments

7

I found a very simple solution that worked for me. This doesn't check for 404's however I had objects that possibly had and .image attribute. I know this isn't the answer to his question but hopefully it helps some one out there.

<img class="list-thumb-img" [attr.src]="item.image?.url ? item.image.url : 'assets/img/140-100.png'">

Comments

6

A perfect Angular 8 directive:

import {AfterViewInit, Directive, ElementRef, Input} from '@angular/core';

@Directive({
    selector: '[appImage]'
})
export class ImageDirective implements AfterViewInit {

    @Input() src;

    constructor(private imageRef: ElementRef) {
    }

    ngAfterViewInit(): void {
        const img = new Image();
        img.onload = () => {
            this.setImage(this.src);
        };

        img.onerror = () => {
            // Set a placeholder image 
            this.setImage('assets/placeholder.png');
        };

        img.src = this.src;
    }

    private setImage(src: string) {
        this.imageRef.nativeElement.setAttribute('src', src);
    }
}

Now, HTML will be:

<img [src]="'/some/valid-image.png'" appImage>

1 Comment

it's not applying directive. I created directive and not worked!!
4

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'; }

Comments

2

this is my solution for fallback on multi images. we basically detach the ChangeDetector once we resolve the image so we can reduce CPU on vm check cycles once image is resolved.

import {Component, ChangeDetectionStrategy, ChangeDetectorRef} from 'angular2/core';
import {AppStore} from "angular2-redux-util/dist/index";



@Component({
    selector: 'logoCompany',
    changeDetection: ChangeDetectionStrategy.Default,
    template: `
            <div style="padding-top: 7px" > 
              <span style="color: gainsboro; font-family: Roboto">{{getBusinessInfo('companyName')}}</span>
              <!--<img style="width: 35px" class="img-circle" src="http://galaxy.example.me/Resources/Resellers/{{getBusinessInfo('businessId')}}/{{getBusinessInfo('fileName')}}" />-->
              <img style="width: 35px" class="img-circle" [src]="getImageUrl()" (load)="onImageLoaded()" (error)="onImageError()" />
            </div>
    `
})

export class LogoCompany {

    constructor(private cdr:ChangeDetectorRef) {    
    }
    private imageRetries:number = 0;
    private unsub;


    private detach() {
        this.cdr.detach();
    }

    private getImageUrl() {          
        var url = '';
        switch (this.imageRetries){
            case 0: {
                url = 'http://galaxy.example.me/Resources/Resellers/' + this.getBusinessInfo('businessId') + '/Logo.jpg'
                break;
            }
            case 1: {
                url = 'http://galaxy.example.me/Resources/Resellers/' + this.getBusinessInfo('businessId') + '/Logo.png'
                break;
            }
            default: {
                url = 'assets/person.png'
                break;
            }

        }
        return url;
    }

    private onImageLoaded() {
        this.detach();
    }

    private onImageError() {
        this.imageRetries++;
    }

    private getBusinessInfo(field):string {
          return '12345';
    }





}

Comments

1

The event of error happen only if the url is not correct, if the url is correct but the file is not exists you can use in this code

javascript check if file exists on server:

(Note that the 404 is a number and not a string)

function doesFileExist(urlToFile) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, false);
xhr.send();
 
if (xhr.status == 404) {
    return false;
} else {
    return true;
}}

Comments

1

Listen to the (error) event of the image element & if image is broken you can hide the image itself:

<img #image src="..." (error)="image.hidden = true" [hidden]="image.hidden">

Or any other element:

<div [hidden]="image.hidden"></div>

Or completely remove any div:

<div *ngIf="!image.hidden"></div>

Or when using *ngFor:

<div *ngFor="let some of somethings">
   <div *ngIf="some.imageUrl" class="image-class-1 img-class-2">
     <img src="{{some.imageUrl}}" (error)="some.imageUrl = null"/>
   </div>
</div>

Comments

0

You can simply use a ternary operator for this use case. I am assuming that you are getting the responses from a remote server

<img src="{{ res.image ? res.image : onErrorFunction() }}"

If you don't want to have the image from server write the path in single quotes ''.

Here if the link is broken then it will go to onErrorFunction if it isn't broken the res.image will be loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.