Directive that works as ng-if (Angular 2)
After some more research I found a great doc about how to build custom directives, specifically the way I need, that acts as an ngIf directive. You can read about it here and see the plunkr here
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';@Directive({ selector: '[isAllow]'})export class isAllowDirective { constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef) {} @Input() set isAllow(allow: boolean){ if (allow) { // If condition is true add template to DOM this.viewContainer.createEmbeddedView(this.templateRef); } else { // Else remove template from DOM this.viewContainer.clear(); } }}
The accepted answer doesn't use angular's *ngIf
implementation.
I've written a custom *ngIf
directive that uses it:http://avenshteinohad.blogspot.com/2018/06/custom-ngif-directive.html