I want to change activated mat-tab in typescript file. Here Programmatically select md-tab in Angular 2 material I found the closest solution but for md-tab not mat-tab.I tried this but didn't work.Here what I tried
HTML
<button class="btn btn-primary" (click)="changeTab()">Click me!</button>
<mat-tab-group [selectedIndex]="selectedTab">
<mat-tab label="Preview">
</mat-tab>
<mat-tab label="Tuning">
<car-tuning></car-tuning>
</mat-tab>
<mat-tab label="Payment"></mat-tab>
</mat-tab-group>
.TS File
changeTab() {
this.selectedTab =1;
}
I want to change activated mat-tab in typescript file. Here Programmatically select md-tab in Angular 2 material I found the closest solution but for md-tab not mat-tab.I tried this but didn't work.Here what I tried
HTML
<button class="btn btn-primary" (click)="changeTab()">Click me!</button>
<mat-tab-group [selectedIndex]="selectedTab">
<mat-tab label="Preview">
</mat-tab>
<mat-tab label="Tuning">
<car-tuning></car-tuning>
</mat-tab>
<mat-tab label="Payment"></mat-tab>
</mat-tab-group>
.TS File
changeTab() {
this.selectedTab =1;
}
Share
edited Dec 28, 2019 at 1:38
Cœur
38.8k26 gold badges205 silver badges277 bronze badges
asked Sep 12, 2019 at 12:31
Timuçin ÇiçekTimuçin Çiçek
1,6263 gold badges24 silver badges51 bronze badges
1
- There's an example on the site showing how to programmatically set the selected tab (fourth one down). – Heretic Monkey Commented Sep 12, 2019 at 12:37
2 Answers
Reset to default 8You can add two way binding
like this [(selectedIndex)]="selectedTab"
and should give you result.
<button class="btn btn-primary" (click)="changeTab()">Click me!</button>
<mat-tab-group [(selectedIndex)]="selectedTab"> //<--- changes
<mat-tab label="Preview">
</mat-tab>
<mat-tab label="Tuning">
<car-tuning></car-tuning>
</mat-tab>
<mat-tab label="Payment"></mat-tab>
</mat-tab-group>
In ts:
changeTab() {
this.selectedTab = 1 //<------- your tab value
}
yes you can change like this.
<mat-tab-group [(selectedIndex)]="selectedIndex">
<mat-tab>
</mat-tab>
<mat-tab>
</mat-tab>
<mat-tab>
</mat-tab>
</mat-tab-group>
In ts use:
selectedIndex: number;
in your function
this.selectedIndex = whateverIndexYouWant;
here is how I change activated index from ts.
this.data.currentMessage.subscribe(message => {
switch (message) {
case 'blabla':
this.selectedIndex = 0;
break;
case 'blablablabla':
this.selectedIndex = 1;
break;
case 'blablablablablabla':
this.selectedIndex = 2;
break;
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743689143a4490659.html
评论列表(0条)