问题
Does ng-template have any events when it is visible or active in the scope or is it possible to execute any method in such way to call it from the container?
<ng-template (active/vissible)="callMethod()"> ... </ng-template>
(Or)
<ng-container *ngTemplateOutlet="mytemplate" ></ng-container>
回答1:
One option could be (valid for any DOM object) is via ViewChildren's QueryList's changes. You mark any DOM element (or use it's type) - <div #myEl></div>
Assign it:
@ViewChildren('myEl') myEl: QueryList<any>;
And subscribe for changes in ngAfterViewInit
(as earlier AFAIK its not yet created):
ngAfterViewInit() {
this.myEl.changes.subscribe(_ => console.log(_));
}
回答2:
You can use
public mycondition: boolean = true; ///according to need make it true or false
<ng-template *ngIf="mycondition"> ... </ng-template>
来源:https://stackoverflow.com/questions/49926222/call-a-method-when-ng-template-is-visible