如何使用按钮返回javascript(Angular Material)的最后一个标签?

时间:2018-08-09 09:32:28

标签: javascript angular angular-material material-design

我的代码摘要

我想如果我单击“返回内容”,则返回内容选项卡1;

<mat-tab-group mat-stretch-tabs>
    <mat-tab label="content 1">
        ........
    </mat-tab>
    <mat-tab label="content 2">
        <button mat-raised-button (click)=""> go Back content 1</button>
    </mat-tab>
</mat-tab-group>

2 个答案:

答案 0 :(得分:1)

您可以将当前所选标签的索引绑定到变量,然后在需要时设置该变量,如下所示:

<mat-tab-group mat-stretch-tabs [selectedIndex]="selectedIndex">
    <mat-tab label="content 1">
        ........
    </mat-tab>
    <mat-tab label="content 2">
        <button mat-raised-button (click)="selectedIndex = 0"> go Back content 1</button>
    </mat-tab>
</mat-tab-group>

您可以阅读更多here

答案 1 :(得分:0)

尝试一下:

<mat-tab-group mat-stretch-tabs [(selectedIndex)]="selectedTab">
    <mat-tab label="content 1">
       <p>one</p>
    </mat-tab>
    <mat-tab label="content 2">
       <p>two</p>
       <button mat-raised-button (click)="openContentOne()"> go Back content 1</button>
   </mat-tab>
</mat-tab-group>

和您的ts中:

selectedTab = 0;
openContentOne() {
    this.selectedTab = 0;
}