Skip to main content

angular material - How to toggle Badge visibility

app.component.ts

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

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


export class AppComponent {
  isBadgeHidden:boolean = false;

  toggleBadgeVisibility(){
    this.isBadgeHidden = !this.isBadgeHidden;
  }
}
app.component.html

<h2 style="padding-top:25px; padding-bottom:25px;">
  Angular Material - How to toggle Badge visibility
</h2>


<h3>Toggle badge visibility on button click</h3>

<button 
  mat-raised-button matBadge="6" 
  [matBadgeHidden]="isBadgeHidden" 
  (click)="toggleBadgeVisibility()">
    {{isBadgeHidden? 'Show Badge' : 'Hide Badge'}}
</button>
app.module.ts [import]

import { MatBadgeModule } from '@angular/material/badge'
import { MatButtonModule } from '@angular/material/button'
More Angular tutorials