4

ng-template 在范围内可见或处于活动状态时是否有任何事件, 或者是否可以以这种方式执行任何方法以从容器中调用它?

<ng-template (active/vissible)="callMethod()"> ... </ng-template>

(或者)

<ng-container *ngTemplateOutlet="mytemplate" ></ng-container>
4

2 回答 2

3

一种选择可能是(对任何 DOM 对象有效)是通过 ViewChildren 的 QueryList 的changes. 您标记任何 DOM 元素(或使用它的类型) -<div #myEl></div>

分配它:

@ViewChildren('myEl') myEl: QueryList<any>;

并订阅更改ngAfterViewInit(因为早期的 AFAIK 尚未创建):

ngAfterViewInit() {
  this.myEl.changes.subscribe(_ => console.log(_));
}
于 2018-04-19T17:52:11.007 回答
-1

您可以使用

public mycondition: boolean = true;   ///according to need make it true or false

<ng-template *ngIf="mycondition"> ... </ng-template>
于 2018-04-19T17:15:37.947 回答