Skip to content

Instantly share code, notes, and snippets.

@wizardnet972
Created January 9, 2018 06:29
Show Gist options
  • Select an option

  • Save wizardnet972/bf0f143914b4f82ab5769da4f9a8dc0f to your computer and use it in GitHub Desktop.

Select an option

Save wizardnet972/bf0f143914b4f82ab5769da4f9a8dc0f to your computer and use it in GitHub Desktop.
image default on error
default-image.directive.ts:
import { Directive, Input, HostListener, ElementRef } from '@angular/core';
@Directive({
selector: 'img[default]',
})
export class DefaultImageDirective {
@Input() default: string;
constructor(private element: ElementRef) { }
@HostListener('error')
updateUrl() {
this.element.nativeElement.attributes['src'].value = this.default;
}
}
in your file:
<img height="32" width="32" default="http://www.freebanking.org/avatars/schuler.png" [src]="user.picture" [alt]="user.fullname">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment