Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. import { Directive, Input, TemplateRef, ViewContainerRef, OnInit, AfterViewInit } from '@angular/core';
  2. import { FeatureService } from '../services/feature.service';
  3.  
  4. /**
  5. * Add the template content to the DOM when feature is enabled
  6. * Usage:
  7. * <div *feature="'exampleFeature'"></div>
  8. */
  9. @Directive({ selector: '[feature]' })
  10. export class FeatureDirective implements OnInit {
  11. @Input() feature: string;
  12. constructor(
  13. private templateRef: TemplateRef<any>,
  14. private viewContainer: ViewContainerRef,
  15. private featureService: FeatureService,
  16. ) {}
  17.  
  18. ngOnInit() {
  19. this.featureService.changes().subscribe((features: any) => {
  20. if (this.featureService.hasFeature(this.feature)) {
  21. this.viewContainer.createEmbeddedView(this.templateRef);
  22. } else {
  23. this.viewContainer.clear();
  24. }
  25. });
  26. }
  27. }
Add Comment
Please, Sign In to add comment