【发布时间】:2016-12-05 02:32:08
【问题描述】:
我有一个 NgSwitch 模板。在 NgSwitch 中,我想获得对初始化模板的模板引用。像这样的:
<div class="container" [ngSwitch]="model.type">
<first-component #ref *ngSwitchCase="0"></first-component>
<second-component #ref *ngSwitchCase="1"></second-component>
<third-component #ref *ngSwitchCase="2"></third-component>
</div>
单击组件中的按钮时,我想将已初始化的组件(第一个/第二个/第三个)调用到一个方法(该方法在所有这 3 个组件实现的接口上定义)。问题是 ViewChild 未定义。如果我将#ref 移动到容器 div,如下所示:
<div class="container" #ref [ngSwitch]="model.type">
<first-component *ngSwitchCase="0"></first-component>
<second-component *ngSwitchCase="1"></second-component>
<third-component *ngSwitchCase="2"></third-component>
</div>
ViewChild(模板引用)已初始化,但随后我可以调用组件的方法。
如何同时使用 NgSwitch 指令和模板引用变量? 或者另一方面,我如何从其父组件调用已初始化的组件(在我将#ref 移动到容器 div 的情况下)。
【问题讨论】:
标签: angular angular2-template ng-switch