How to get the active tab In Angular Material2

后端 未结 4 381
不思量自难忘°
不思量自难忘° 2020-12-05 02:13

I want to get which tab is active. I tried to use a @ViewChild decorator and accessing the element properties that way, but it returns null.

<
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 02:50

    "@angular/material": "^6.2.1". The way to get selected tab index on-load (after template has loaded) and when the tab is focused.

    my.component.ts

    export class MyComponent implements OnInit, AfterViewInit {
        @ViewChild('tabGroup') tabGroup;
    
        ngAfterViewInit() {
          console.log(this.tabGroup.selectedIndex);
        }
    
        public tabChanged(tabChangeEvent: MatTabChangeEvent): void {
          console.log(tabChangeEvent);
        }
    }
    

    my.component.html

    
        
            Tab 1
            Tab Content
        
        
            Tab 2
            Tab Content
        
    
    

提交回复
热议问题