I am facing issue on Angular project about remove broken image and I removed successfully for Chrome
,Firefox
, and Edge
. But it is not working for IE
(Internet Explorer - I am using version 11).
The scenario is that both logoURL
and backUpImage
reponse no image.
What I have did to fix this issue:
- create
image-default.directive
to handle image link in case of first url is failed - add
alt=""
Here is my code:
image-default.directive.ts
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;
@Input() class: string = 'agent-img';
@HostBinding('class') className
updateUrl() {
this.src = this.default;
}
load() {
this.className = this.class;
}
}
- HTML applied
<li *ngIf="logoURL" class="logo">
<a
href="{{companyWebsite}}"
target="_blank"
queryParamsHandling="merge"
routerLinkActive="active"
[routerLinkActiveOptions]="{exact :true}">
<img src="{{logoURL}}" width="150" default="{{backUpImage}}" alt="">
</a>
</li>
Here is result on IE 11
What I am looking for:
Is there any way to fix it by using CSS in case of main url and backup url is failed?