How to get the active tab In Angular Material2

后端 未结 4 382
不思量自难忘°
不思量自难忘° 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:36

    Well, I'm not sure if I understood well your question because, by default the index always starts counting from zero unless you set manually the [selectedIndex] property.

    Anyway, if you really want to see which tab is selected on initialization you could implement the AfterViewInit interface and do the following:

    export class AppComponent implements AfterViewInit, OnInit {
    
    ...
    
      ngAfterViewInit() {
        console.log('afterViewInit => ', this.tabGroup.selectedIndex);
      }
    }
    

    On other hand, if you want to check which tab is selected based on changes (what makes more sense), here you go:

    HTML:

    <mat-tab-group #tabGroup (selectedTabChange)="tabChanged($event)">
      <mat-tab label="Tab 1">Content 1</mat-tab>
      <mat-tab label="Tab 2">Content 2</mat-tab>
    </mat-tab-group>
    

    Component:

    tabChanged(tabChangeEvent: MatTabChangeEvent): void {
      console.log('tabChangeEvent => ', tabChangeEvent);
      console.log('index => ', tabChangeEvent.index);
    }
    

    DEMO

    0 讨论(0)
  • 2020-12-05 02:41

    This is how you can get active index of active angular material tabs

    .html file

    <div>    
    <mat-tab-group #tabRef (selectedTabChange)="logChange(tabRef.selectedIndex)">
      <mat-tab label="ALL"></mat-tab>
      <mat-tab label="Delivered"></mat-tab>
      <mat-tab label="Pending"></mat-tab>
      <mat-tab label="Cancelled"></mat-tab>
    </mat-tab-group>    
    </div>
    

    .ts file

    logChange(index)
    {
      console.log(index);
    } 
    

    Don't forget to add import in app.module.ts file

    import { MatTabsModule } from '@angular/material/tabs';

    0 讨论(0)
  • 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

    <mat-tab-group #tabGroup (focusChange)="tabChanged($event)" [selectedIndex]="1">
        <mat-tab>
            <ng-template mat-tab-label>Tab 1</ng-template>
            Tab Content
        </mat-tab>
        <mat-tab>
            <ng-template mat-tab-label>Tab 2</ng-template>
            Tab Content
        </mat-tab>
    </mat-tab-group>
    
    0 讨论(0)
  • 2020-12-05 02:57

    According to Material documentation Angular Material tabs output an event on tab change @Output() selectedTabChange: EventEmitter<MatTabChangeEvent>

    Your Template:

    <mat-tab-group (selectedTabChange)="tabChanged($event)">
      <mat-tab>
        <ng-template mat-tab-label>Tab 1</ng-template>
        Tab Content
      </mat-tab>
      <mat-tab>
        <ng-template mat-tab-label>Tab 2</ng-template>
        Tab Content
      </mat-tab>
    </mat-tab-group>
    

    Your Typescript:

    import { MatTabChangeEvent } from '@angular/material';
    
    public tabChanged(tabChangeEvent: MatTabChangeEvent): void {
        console.log(tabChangeEvent);
    }
    
    0 讨论(0)
提交回复
热议问题