Skip to content

Instantly share code, notes, and snippets.

@whisher
Created November 15, 2018 20:08
Show Gist options
  • Save whisher/c757e1d7bdb696eb439c51ae210b9426 to your computer and use it in GitHub Desktop.
Save whisher/c757e1d7bdb696eb439c51ae210b9426 to your computer and use it in GitHub Desktop.
import {
Directive,
Renderer2,
TemplateRef,
ViewContainerRef,
Input
} from '@angular/core';
@Directive({
selector: '[appMy]'
})
export class MyDirective {
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private renderer: Renderer2) { }
@Input() set appMy(condition: boolean) {
if (condition) {
const view = this.viewContainer.createEmbeddedView(this.templateRef);
this.renderer.addClass(view.rootNodes[0], 'myclass');
} else {
this.viewContainer.clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment