Material 当 selectedIndex 高于实际标签数时,标签更改为最后一个标签

Material tabs change to last tab when selectedIndex is higher than the actual number of tabs

例如,我的 mat-tab-group 中有 2 个标签:

<mat-tab-group>
  <mat-tab>
  </mat-tab>
  <mat-tab>
  </mat-tab>
</mat-tab-group>

假设选择了第一个选项卡(索引 0),当我将 selectedIndex 更改为 2(应该切换到选项卡 3 但它不存在)时,选择了第二个选项卡。我希望第一个选项卡保持选中状态,因为索引超出范围。我怎样才能做到这一点? (对于一般情况:n 个选项卡,其中一个被选中(不一定是第一个或最后一个,并且 selectedIndex>n

试试这个:

在 TS 中:

let index = this.Activeroute.snapshot.queryParams['index'];

if(selectedIndex > n)
{
   this.selectedIndex = 0;
}
else
{
   this.selectedIndex = index;
}