Skip to content

Instantly share code, notes, and snippets.

@NBprojekt
Last active October 10, 2019 11:25
Show Gist options
  • Save NBprojekt/60f849ba889dd23d7889141eef7088a0 to your computer and use it in GitHub Desktop.
Save NBprojekt/60f849ba889dd23d7889141eef7088a0 to your computer and use it in GitHub Desktop.
Replace broken img with default img directive in Angular
// 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