Kyoo/front/projects/host/src/app/misc/fallback.directive.ts
2021-11-21 19:56:07 +01:00

32 lines
621 B
TypeScript

import { Directive, ElementRef, HostListener, Input, Pipe, PipeTransform } from "@angular/core";
/* eslint-disable @angular-eslint/directive-selector */
@Directive({
selector: "img[fallback]"
})
export class FallbackDirective
{
@Input() fallback: string;
constructor(private img: ElementRef) { }
@HostListener("error")
onError(): void
{
const html: HTMLImageElement = this.img.nativeElement;
html.src = this.fallback;
}
}
@Pipe({
name: "fallback",
pure: true
})
export class FallbackPipe implements PipeTransform
{
transform(value: any, ...args: any[]): any
{
return value ?? args.find(x => x);
}
}