Created
January 9, 2018 06:29
-
-
Save wizardnet972/bf0f143914b4f82ab5769da4f9a8dc0f to your computer and use it in GitHub Desktop.
image default on error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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