Skip to main content

angular material - How to create & add Tab programmatically

app.component.ts

import { Component } from '@angular/core';

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


export class AppComponent { 
  tabs = ["SQL","C#"];

  addTab(){
    this.tabs.push(
      "New Tab " + (this.tabs.length + 1)
    )
  }
}
app.component.html

<h2 style="padding-top:25px; padding-bottom:25px;">
    Angular Material - How to create and add Tab programmatically
</h2>


<h3>Selected Tab Index : {{tabGroup.selectedIndex}}</h3>

<mat-tab-group 
    [selectedIndex]="0" 
    #tabGroup 
    style="margin-bottom:50px;">
        <mat-tab 
            *ngFor="let tab of tabs"
            [label]="tab">
            Content of {{tab}}
        </mat-tab>
</mat-tab-group>

<button 
    mat-raised-button
    color="primary"
    (click)="addTab()">
        Add New Tab
</button>
app.module.ts [import]

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