首页 > 解决方案 > 如何访问我的 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

标签: angular

解决方案


ViewChild 指令?例如

@ViewChild('myFirstTab') myFirstTab: MatTab;
@ViewChild('mySecondTab') mySecondTab: MatTab;

推荐阅读