import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; import { Loaded, LoadingState } from './loading-state'; @Directive({ selector: '[appIfLoaded]' }) export class IfLoadedDirective { private isViewCreated = false; @Input('appIfLoaded') set state(state: LoadingState) { if (!this.isViewCreated && state.type === 'loaded') { this.viewContainerRef.createEmbeddedView(this.templateRef); this.isViewCreated = true; } else if (this.isViewCreated && state.type !== 'loaded') { this.viewContainerRef.clear(); this.isViewCreated = false; } } constructor( private readonly viewContainerRef: ViewContainerRef, private readonly templateRef: TemplateRef ) {} static ngTemplateGuard_appIfLoaded( dir: IfLoadedDirective, state: LoadingState ): state is Loaded { return true; } }