Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a custom class to an Angular Material tab?

In Angular 6, I am using Angular Material to display some data using Tabs.

<mat-tab-group>
    <mat-tab 
    *ngFor="let bar of foo.bar" 
    [label]="bar.Name"
    [ngClass]="bar.IsActive ? 'bar-on' : 'bar-off'">
      // ...
    </mat-tab>
  </mat-tab-group>

I would like to style the tabs differently whether the IsActive property of bar is true or false.

I have tried using [ngClass]="bar.IsActive ? 'bar-on' : 'bar-off'" as shown above, but the class is not added.

Out of spite, I have tried the much simpler class = 'bar-on' but even then the class is not added.

Therefore, I am wondering, is it possible to add a custom class to an Angular Material tab?

Edit: The IsActive property is a property of foo objects. It has nothing to do with the activated state of the tab. The active tab can display an object with IsActive = false and vice-versa.

like image 606
Maxime Avatar asked Jul 11 '18 07:07

Maxime


People also ask

What is Mat tab in angular?

Tabs and navigation While <mat-tab-group> is used to switch between views within a single route, <nav mat-tab-nav-bar> provides a tab-like UI for navigating between routes.

How do you stop mat tabs changing?

How to stop animation in mat tab? Controlling the tab animation If you want to disable the animation completely, you can do so by setting the properties to 0ms .

How do I add tabs in Angularjs 11?

Adding Angular 11 Tab You can add the Angular 11 Tab component by using `ejs-tab` directive and the attributes used within this tag allows you to define other tab functionalities. Import the TabModule into app. module. ts file from the ej2-angular-navigations package.


1 Answers

Yes its possible by using ng-template instead of label.

Example of ngClass on first tab. stackblitz

<mat-tab-group>
  <mat-tab > 
   <ng-template mat-tab-label>
                <span [ngClass]="{'color-red': isError?'red':'black'}">Security</span>
   </ng-template>
    Content 1 
   </mat-tab>
  <mat-tab label="Second"> Content 2 </mat-tab>
  <mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>
like image 137
dasfdsa Avatar answered Sep 21 '22 23:09

dasfdsa