- Badges are small status descriptors for UI elements.
- Example : Displaying notification count for a user over an inbox icon or number of unread messages in a chat.
- Documentation : https://material.angular.io/components/badge/overview
material.module.ts (a new module created)
import { NgModule } from '@angular/core'; import { MatButtonModule } from '@angular/material'; import { MatBadgeModule } from '@angular/material/badge'; import { MatButtonToggleModule} from '@angular/material/button-toggle'; import { MatIconModule} from '@angular/material/icon'; const MaterialComponent = [ MatButtonModule, MatButtonToggleModule, MatIconModule, MatBadgeModule ]; @NgModule({ imports: [ MaterialComponent, ], exports: [ MaterialComponent ] }) export class MaterialModule { }
app.module.ts
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MaterialModule } from './material/material.module'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, MaterialModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
app.component.html
<div style="padding-top: 15px;"> <span matBadge="5">Notifications</span> </div> <div><span matBadge="5" matBadgePosition="below before">Notification</span></div> <div><span matBadge="5" matBadgePosition="below after">Notification</span></div> <div><span matBadge="5" matBadgePosition="above before">Notification</span></div> <div><span matBadge="5" matBadgeSize="small">Notification</span></div> <div><span matBadge="5" matBadgeSize="medium">Notification</span></div> <div><span matBadge="5" matBadgeSize="large">Notification</span></div> <div><span matBadge="5" matBadgeColor="primary">Notification</span></div> <div><span matBadge="5" matBadgeColor="accent">Notification</span></div> <div><span matBadge="5" matBadgeColor="warn">Notification</span></div> <div><span matBadge="5" matBadgeOverlap="false">Notification</span></div> <div><span [matBadge]="notifications">Notification</span></div> <div><span [matBadge]="notifications2" [matBadgeHidden]="notifications2===0" >Notification</span></div>
app.component.css
div{ margin: 1rem; }
Output :