вы можете написать свой собственный ngIf, например, myIf
import { Directive, Input, ElementRef, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[myIf]'
})
export class MyIfDirective {
constructor(
private element: ElementRef,
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef
) {
}
@Input()
set myIf(val) {
if(val) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
}
затем
<div *myIf="isVisible">
Hi there!
</div>
, иначе это поможет
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
import {NgIf} from "@angular/common"
@Directive({
selector: '[ngIf][myNgIf]'
})
export class myNgIf extends NgIf {
@Input() myNgIf: boolean;
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef) {
super(viewContainer, templateRef);
}
@Input()
set ngIf(val) {
if (val) {
...
} else {
this.viewContainer.clear();
}
}