If you want to utilize the [selectedIndex]
@Input feature for managing tabs within a mat-tab-group
, here's how you can do it:
In your Component:
selectedIndex = 0;
selectTab(index: number): void {
this.selectedIndex = index;
}
In your Template:
<mat-tab-group [selectedIndex]="selectedIndex">
<mat-tab label="Tab 1">Content 1</mat-tab>
<mat-tab label="Tab 2">Content 2</mat-tab>
</mat-tab-group>
<button mat-button (click)="selectTab(0)">Show Tab 1</button>
<button mat-button (click)="selectTab(1)">Show Tab 2</button>
Check out the STACKBLITZ DEMO
Alternatively, you can also create a reference to the mat-tab-group
and manipulate it directly in the template like this:
<mat-tab-group #tabGroup>
<mat-tab label="Tab 1">Content 1</mat-tab>
<mat-tab label="Tab 2">Content 2</mat-tab>
</mat-tab-group>
<button mat-button (click)="tabGroup.selectedIndex = 0">Show Tab 1</button>
<button mat-button (click)="tabGroup.selectedIndex = 1">Show Tab 2</button>
Try out the STACKBLITZ DEMO