So I might have missed something obvious in the docs, but I'm simply looking for an easy way to find which tab has been selected in the following scenario:
View
<mat-tab-group (selectedTabChange)="onTabChange($event)">
<mat-tab *ngIf="true" label="Label1">Content 1</mat-tab>
<mat-tab *ngIf="false" label="Label2">Content 2</mat-tab>
<mat-tab *ngIf="true" label="Label3">Content 3</mat-tab>
</mat-tab-group>
Obviously, the conditions are dynamic in my actual code. This is just for the sample.
Script
onTabChange(event: MatTabChangeEvent) {
// ?
}
Issue
In the above script, event.index
will return 0 if I click the first tab, and 1 if I click the third tab, because the second one isn't displayed due to *ngIf
.
This does make sense to me, however it's making it really hard to know which tab was actually clicked depending on which ones were displayed to begin with.
I could either:
- test again for every tab condition within
onTabChange
to figure out which index corresponds to which tab, - (probably) bind a
ViewChild(ren)
reference to something and retrieve data (such asdata-
attributes) through their native DOM elements.
Both these options seem super overkill though.
Question
In the above code, what would be the appropriate way to know that, say, the tab labelled Label3
was clicked, without testing for the label itself (which would obviously be horrendous)?
Simple Stackblitz if that can help.