I have a directive for images, where if an image does not load I display a default image.
Policy:
import { Directive, Input, HostBinding } from '@angular/core';
@Directive({
selector: 'img[default]',
host: {
'(error)':'updateUrl()',
'(load)': 'load()',
'[src]':'src'
}
})
export class ImagePreloadDirective {
@Input() src:string;
@Input() default:string;
@HostBinding('class') className
updateUrl() {
this.src = this.default;
this.className = 'image-default';
}
load(){
//this.className = 'image-default';
}
}
Usage:
<img
[src]="'assets/imgs/img-name.png"
default="assets/imgs/image-default.png">
However, I would like to do the same for another situation where I use background-image
.
<figure [ngStyle]="{'background-image': 'url(assets/imgs/teste_344x120-.jpg)'}"></figure>
However, I'm not able to change or adapt the policy I already have to work when background-image
does not load.