-
-
Save gparlakov/dd9a02f07201f1d554fde6ac43be76a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, Input, TemplateRef } from '@angular/core'; | |
@Directive({ | |
selector: '[ifFeature]', | |
}) | |
export class IfFeatureFlagDirective<IfTemplate, ElseTemplate> { | |
constructedForFlag?: string; | |
elseTemplate?: TemplateRef<ElseTemplate>; | |
flag?: string; | |
@Input('ifFeature') | |
set ifFeatureFlag(flag: string) { | |
this.flag = flag; | |
this.updateView(); | |
} | |
@Input('ifFeatureElse') | |
set ifFeatureFlagNotPresent(t: TemplateRef<ElseTemplate>) { | |
this.elseTemplate = t; | |
} | |
private updateView() { | |
this.flags.checkIsOn(this.flag).subscribe((on) => { | |
this.viewContainerRef.clear(); | |
if (on) { | |
this.viewContainerRef.createEmbeddedView(this.template); | |
} else if (this.elseTemplate != null) { | |
this.viewContainerRef.createEmbeddedView(this.elseTemplate); | |
} else { | |
this.viewContainerRef.clear(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment