Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Created March 9, 2018 15:55
Show Gist options
  • Save amcdnl/998ecc25ec1fda6615aeabe81faa654c to your computer and use it in GitHub Desktop.
Save amcdnl/998ecc25ec1fda6615aeabe81faa654c to your computer and use it in GitHub Desktop.
import { Directive, Input, TemplateRef, ViewContainerRef, OnInit } from '@angular/core';
import { CONFIG } from 'environments/config';
@Directive({
selector: '[featureToggle]'
})
export class FeatureToggleDirective implements OnInit {
@Input() featureToggle: string;
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef
) {}
ngOnInit() {
if (this.isEnabled()) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
isEnabled() {
const { features } = CONFIG;
if (features['*']) {
return true;
}
return features[this.featureToggle];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment