angular - 如何访问我的 ViewChildren 的模板参考?
问题描述
考虑我的模板中的这些材料选项卡:
<mat-tab #myFirstTab>Something</mat-tab>
<mat-tab #mySecondTab>Something else</mat-tab>
以及 .ts 文件中的 ViewChildren:
@ViewChildren(MatTab)
private allTabs: QueryList<MatTab>;
public ngAfterViewInit() {
this.allTabs.forEach(tab => console.log('tab: ', tab));
}
如何在我的 .ts 代码中访问每个选项卡(#myFirstTab 和 #mySecondTab)的实际模板引用?有可能吗?我需要一种方法来区分我的标签,而标签文本不好,因为它可能会有所不同......
编辑:我特别要求@ViewChildren 而不是@ViewChild。我当然知道反过来。在这种情况下,我不想在我的 .ts 文件中声明每个 Viewchild。
解决方案
ViewChild 指令?例如
@ViewChild('myFirstTab') myFirstTab: MatTab;
@ViewChild('mySecondTab') mySecondTab: MatTab;
推荐阅读
- c# - 在 C# 和 Unity 中使用 input.getkeydown(KeyCode.Space) 创建游戏对象的副本时遇到问题
- kubernetes - 同一应用程序的多个服务:kubernetes 中的端口
- youtube - 视频所有者已禁用在其他网站上的播放(但不是)
- javascript - 为什么我的 Firestore/Firebase 添加文档功能在生产中工作,而不是本地机器?
- jquery - 如何在某些函数中获取数据表加载时带有逗号分隔值的列列表
- php - 如何在codeigniter中修复网站的url?
- google-maps - 在 Xamarin 表单中动态设置 Google Maps API 密钥
- ansible - 如何在剧本中执行多个任务时将每个输出存储在文件中?
- amazon-web-services - 如何强制 aws-cli 或 terraform 删除 OpsWorks?
- javascript - 用 d3 / svg 画一个曲折的圆