Angular Material is a popular UI framework based on Material Design for Angular.
In this article, we’ll look at how to use Angular Material into our Angular project.
Badges
We can add badges into our app with Angular Material.
To add it, we write:
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatBadgeModule } from '@angular/material/badge';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatBadgeModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
<div>
<span matBadge="4" matBadgeOverlap="false">Text with a badge</span>
</div>
We add the matBadge
and matBadgeOverlap
properties to add a badge.
Also, we can make it raised:
app.component.html
<div>
<span mat-raised matBadge="4" matBadgeOverlap="false">Text with a badge</span>
</div>
Bottom Sheet
A bottom sheet adds a panel to the bottom of the screen.
For example, we can use it by writing:
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatBottomSheetModule } from '@angular/material/bottom-sheet';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatBottomSheetModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
import { MatBottomSheet, MatBottomSheetRef } from '@angular/material/bottom-sheet';
@Component({
selector: 'bottom-sheet-overview-example-sheet',
template: 'bottom sheet',
})
export class BottomSheetOverviewExampleSheet {
constructor(private _bottomSheetRef: MatBottomSheetRef<BottomSheetOverviewExampleSheet>) {}
openLink(event: MouseEvent): void {
this._bottomSheetRef.dismiss();
event.preventDefault();
}
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private _bottomSheet: MatBottomSheet) { }
openBottomSheet(): void {
this._bottomSheet.open(BottomSheetOverviewExampleSheet);
}
}
app.component.html
<div>
<button mat-raised-button (click)="openBottomSheet()">Open file</button>
</div>
We added a button to that calls the openBottomSheet
method to open the bottom sheet.
In app.component.ts
, we added a BottomSheetOverviewExampleSheet
component to show the sheet.
We have the openBottomSheet
method to show the sheet.
We inject the _bottomSheet
service so that we can open the bottom sheet.
Button
We can add buttons with the MatButtonModule
.
For instance, we can write:
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatButtonModule } from '@angular/material/button';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatButtonModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
<div>
<button mat-button color="primary">Primary</button>
<button mat-raised-button color="primary">Primary</button>
</div>
We add the mat-button
and mat-raised-button
directives to add Material design buttons.
Conclusion
We can add badges, bottom sheets, and buttons with Angular Material easily.