我有两个标签和两个按钮
<md-tab-group>
<md-tab label="Tab 1">Content 1</md-tab>
<md-tab label="Tab 2">Content 2</md-tab>
</md-tab-group>
<button md-button (click)="showTab1()">Show Tab 1</button>
<button md-button (click)="showTab2()">Show Tab 2</button>
如果我在Tab 1上,我需要函数showTab1()切换到选项卡1,如果单击该按钮,我需要执行showTab2()。有人可以帮忙吗?
答案 0 :(得分:13)
您可以[selectedIndex]
使用mat-tab-group
@Input :
<强>组件:强>
selectedIndex = 0;
selectTab(index: number): void {
this.selectedIndex = index;
}
<强>模板:强>
<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>
...或者您可以创建对mat-tab-group
的引用并直接在模板中对其进行操作:
<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>
答案 1 :(得分:0)
您需要声明一个变量,例如tab: number = 1
。然后,您可以创建模板,如:
<md-tab-group>
<md-tab *ngIf="tab==1" label="Tab 1">Content 1</md-tab>
<md-tab *ngIf="tab==2" label="Tab 2">Content 2</md-tab>
</md-tab-group>
<button md-button (click)="tab=1">Show Tab 1</button>
<button md-button (click)="tab=2">Show Tab 2</button>