Open In App

matBadge in angular-material

Angular Material is a UI component library that is developed by the Angular team to build design components for desktop and mobile web applications. In order to install it, we need to have angular installed in our project, once you have it you can enter the below command and can download it. 

The main use of the matBadge and assign a value in order to display numbers or status over text or images or icons. We can relate to them as a notification icon number indicator.



Installation syntax:

ng add @angular/material

Approach:



Code Implementation:

app.module.ts:




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
  
import { AppComponent } from './app.component';
import { MatBadgeModule} from '@angular/material/badge'
import { MatIconModule} from '@angular/material/icon'
import { MatButtonModule} from '@angular/material/button'
  
@NgModule({
  imports:
  [ BrowserModule, 
    FormsModule,
    MatButtonModule, 
    MatBadgeModule,
    MatIconModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.html:




<h3> Mat-Badge over a text</h3>
  
<p>
    <span matBadge="8" matBadgeOverlap="false">
        Text with a badge
    </span>
</p>
<hr>
  
<h3>
    Mat-Badge positioned left 
    and right over a Button
</h3>
  
<p>
    <button mat-raised-button color="primary"
        matBadge="8" matBadgePosition="before" 
        matBadgeColor="accent">
        Left badge Button with accent theme
    </button>
</p>
  
<p>
    <button mat-raised-button color="accent" 
        matBadge="8" matBadgePosition="after" 
        matBadgeColor="primary">
        Right badge button with primary theme
    </button>
</p>
<hr>
  
<h3> Mat-Badge over a icon</h3>
  
<p>
    <mat-icon matBadge="8" matBadgeColor="warn">
        home
    </mat-icon>
</p>

Output:


Article Tags :