Skip to main content

angular material - How to use Tabs selectedTabChange event

app.component.ts

import { Component } from '@angular/core';
import { MatTabGroup } from '@angular/material/tabs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})


export class AppComponent { 
  message:string = "";

  logTabChange(tabs:MatTabGroup){
    tabs.selectedIndex
    this.message = "Tab selection changed. Selected index is : "
     + tabs.selectedIndex
  }
}
app.component.html

<h2 style="padding-top:25px; padding-bottom:25px;">
    Angular Material - How to use Tabs selectedTabChange event
</h2>


<h3>{{message}}</h3>

<mat-tab-group #tabs (selectedTabChange)="logTabChange(tabs)">
    <mat-tab label="Flash">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Nam tincidunt elit non molestie rutrum.
        Donec blandit risus et sem iaculis aliquam.
    </mat-tab>

    <mat-tab label="Dreamweaver">
        Cras feugiat pharetra dolor, ac euismod tellus maximus eu.
        Etiam dignissim enim vel lacus molestie, in iaculis tellus
        ultrices.
    </mat-tab>

    <mat-tab label="Photoshop">
        Integer rhoncus tristique felis ut dignissim.
    </mat-tab>
</mat-tab-group>
app.module.ts [import]

import { MatTabsModule } from '@angular/material/tabs'
More Angular tutorials