Call a method when ng-template is visible

痴心易碎 提交于 2019-12-24 02:19:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!