import { Directive, Input, OnInit, TemplateRef, ViewContainerRef } from '@angular/core'; import { TokenService } from '@core/services/token/token.service'; @Directive({ selector: '[gcHideForImpersonation]' }) export class HideForImpersonationDirective implements OnInit { @Input() gcHideForImpersonation = true; constructor ( public templateRef: TemplateRef, public viewContainerRef: ViewContainerRef, private tokenService: TokenService ) { } ngOnInit () { // if impersonating and something we want to hide if ( this.tokenService.hasImpersonationToken() && this.gcHideForImpersonation ) { this.viewContainerRef.clear(); // if impersonating and something we want to show } else if ( this.tokenService.hasImpersonationToken() && !this.gcHideForImpersonation ) { this.viewContainerRef.createEmbeddedView( this.templateRef ); // if not impersonating and something we want to hide } else if ( !this.tokenService.hasImpersonationToken() && !this.gcHideForImpersonation ) { this.viewContainerRef.clear(); } else { this.viewContainerRef.createEmbeddedView( this.templateRef ); } } }