Programmatically select mat-tab in Angular 2 material using mat-tab-group

前端 未结 4 1796
误落风尘
误落风尘 2020-12-13 23:53

How can I select a specific tab when an event occurs?

I tried with [selectedIndex]=\"selectedTab\" changing the selectedTab to the tab ind

4条回答
  •  情歌与酒
    2020-12-14 00:36

    I also had similar issue. In my case I needed to show the tab the user was there before he left the component. I solved this by stuffing the current selected tab index in a service.

    On HTML template I have this:

    
    

    Implementation of onTabChange and getSelectedIndex are as follows:

        getSelectedIndex(): number {
            return this.appService.currentTabIndex
        }
    
        onTabChange(event: MatTabChangeEvent) {
            this.appService.currentTabIndex = event.index
        }
    

    My service code looks like this:

    export class AppService {
        public currentTabIndex = 1  //default tab index is 1
    }
    

提交回复
热议问题