Last active
October 10, 2019 11:25
-
-
Save NBprojekt/60f849ba889dd23d7889141eef7088a0 to your computer and use it in GitHub Desktop.
Replace broken img with default img directive in Angular
This file contains 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
// Usage: | |
// <img src="broken.jpg" default="placeholder.jpg"> | |
import { Directive, Input } from '@angular/core' | |
@Directive({ | |
selector: 'img[default]', | |
host: { | |
'(error)':'updateUrl()', | |
'[src]':'src' | |
} | |
}) | |
export class ErrorImgDirective { | |
@Input() src:string; | |
@Input() default:string; | |
updateUrl() { | |
this.src = this.default; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment