Created
November 25, 2021 13:56
-
-
Save eneajaho/540f279686fede9160faa087a9bdd4c9 to your computer and use it in GitHub Desktop.
Default image on error - Angular directive
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
import { Directive, HostListener, Input, NgModule } from '@angular/core'; | |
@Directive({ | |
selector: '[defaultImageOnError]' | |
}) | |
export class DefaultImageOnErrorDirective { | |
@Input() defaultImageOnError: string; | |
@HostListener('error', ['$event']) | |
handleError(event: ErrorEvent): void { | |
(event.target as HTMLImageElement).src = this.defaultImageOnError; | |
} | |
} | |
@NgModule({ | |
exports: [DefaultImageOnErrorDirective], | |
declarations: [DefaultImageOnErrorDirective], | |
}) | |
export class DefaultImageOnErrorDirectiveModule {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: