Search code examples
angularangular-materialmat-tab

How to show material tab content only if is active?


I trying to show tab content only if is selected:

        <mat-tab label="contacts">
            <p-contacts [contacts]="selectedPanel.contacts"
                        *ngIf="tabGroup.selectedIndex === 1">
            </p-contacts>
        </mat-tab>

it is work, but i got ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: false'. Current value: 'ngIf: true'. So what do i did wrong?

demo


Solution

  • You can lazily load the tabs' content by put the content in ng-template with matTabContent attribute like this:

    <mat-tab-group  #tabGroup>
      <mat-tab  label="Firt">
        <ng-template matTabContent>
          Content 1
        </ng-template>
      </mat-tab>
      <mat-tab  label="Second">
        <ng-template matTabContent>
          Content 2
        </ng-template>
      </mat-tab>
      <mat-tab  label="Third">
        <ng-template matTabContent>
          Content 3
        </ng-template>
      </mat-tab>
    </mat-tab-group>