Getting Started with Angular Material
by Vihan Gammanpila / January 23, 2024
This article will provide an overview of how to get started with Angular Material, a UI component library that implements Material Design in Angular. This article details the process of configuring an Angular application for incorporating Angular Material. It includes steps to showcase the utilization of material components, as illustrated in the following sections.
Set up
Generate the app
Generate a new Angular app and add Angular Material.
- Use Angular CLI to generate a new Angular App
Run ng new my-app
in the command line, replacing my-app
with the name of the app.
2. Add Angular Material
Run the following commands
cd my-app
ng add @angular/material
Remove defaults
The current content is auto-generated by Angular in the first step, but we want to create our own.
Remove everything in app.component.html
except for <router-outlet></router-outlet>
.
Create the material module
First, to use material components in our app, they must be imported. Make a new file named material.module.ts
. This is where you'll bring in each component. After that, this material module gets added to our app.
It’s possible, but not suggested, to add material components straight into the app.module.ts
file.
For now, we’ll bring in every component Angular Material offers. This way, everything’s set up and ready. Later, you can change this to include only what you need.
Here’s what to do:
- In
src\app
, make a new folder namedmaterial
. - Inside this, make a file called
material.module.ts
. Put the following details in this file."
import { NgModule } from '@angular/core';
import { A11yModule } from '@angular/cdk/a11y';
import { ClipboardModule } from '@angular/cdk/clipboard';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { PortalModule } from '@angular/cdk/portal';
import { ScrollingModule } from '@angular/cdk/scrolling';
import { CdkStepperModule } from '@angular/cdk/stepper';
import { CdkTableModule } from '@angular/cdk/table';
import { CdkTreeModule } from '@angular/cdk/tree';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatBadgeModule } from '@angular/material/badge';
import { MatBottomSheetModule } from '@angular/material/bottom-sheet';
import { MatButtonModule } from '@angular/material/button';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatCardModule } from '@angular/material/card';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatChipsModule } from '@angular/material/chips';
import { MatStepperModule } from '@angular/material/stepper';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatDialogModule } from '@angular/material/dialog';
import { MatDividerModule } from '@angular/material/divider';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatGridListModule } from '@angular/material/grid-list';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatListModule } from '@angular/material/list';
import { MatMenuModule } from '@angular/material/menu';
import { MatNativeDateModule, MatRippleModule } from '@angular/material/core';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatRadioModule } from '@angular/material/radio';
import { MatSelectModule } from '@angular/material/select';
import { MatSidenavModule } from '@angular/material/sidenav';
import { MatSliderModule } from '@angular/material/slider';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { MatTabsModule } from '@angular/material/tabs';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatTreeModule } from '@angular/material/tree';
@NgModule({
exports: [
A11yModule,
ClipboardModule,
CdkStepperModule,
CdkTableModule,
CdkTreeModule,
DragDropModule,
MatAutocompleteModule,
MatBadgeModule,
MatBottomSheetModule,
MatButtonModule,
MatButtonToggleModule,
MatCardModule,
MatCheckboxModule,
MatChipsModule,
MatStepperModule,
MatDatepickerModule,
MatDialogModule,
MatDividerModule,
MatExpansionModule,
MatGridListModule,
MatIconModule,
MatInputModule,
MatListModule,
MatMenuModule,
MatNativeDateModule,
MatPaginatorModule,
MatProgressBarModule,
MatProgressSpinnerModule,
MatRadioModule,
MatRippleModule,
MatSelectModule,
MatSidenavModule,
MatSliderModule,
MatSlideToggleModule,
MatSnackBarModule,
MatSortModule,
MatTableModule,
MatTabsModule,
MatToolbarModule,
MatTooltipModule,
MatTreeModule,
PortalModule,
ScrollingModule,
]
})
export class MaterialModule { }
This file takes in every material component available. Then, it puts them in a MaterialModule. This module makes them accessible to our app.
Import the material module
In app.module.ts
, an import statement to the material module created in the last step.
Finally, put MaterialModule
into the imports list of the AppModule
. This step actually imports it into the app.
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 { MaterialModule } from './material/material.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MaterialModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
The app is all set to work with Angular Material. In the following step, we’ll add some content to the app. This will show how the material components look and function.
Add content
Create a component
- Generate a component using Angular CLI
Run ng generate component home
2. Set up the routes
Replace the empty routes in app.routing.module.ts
with
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: '**', redirectTo: '/', pathMatch: 'full' }
];
These steps have created a component called Home
and set this as the base page.
To see it in action, just run ng serve
. This compiles the app, and you should see the message 'home works!
Create a header toolbar bar
In app.component.html
add a toolbar above the container div to be our app header
<mat-toolbar class="mat-elevation-z6" color="primary"></mat-toolbar>
Run ng-serve
and open your browser to see the header at the top of the app.
We’ve successfully incorporated our initial Angular Material component. These components have a style that matches the theme. We chose the theme’s primary color for the bar by using the color attribute. Also, we used material elevation classes to give the components a shadowed, textured appearance.
Add Angular Material Components
Swap out the current contents in home.component.html
with the code provided below. This code includes examples of material components. After that, refresh your browser. This will let you see the Angular Material components working.
<h1 class="mat-display-1">Easy, Breezy, Beautiful: Angular Material</h1>
<p>Here are some random angular material components</p>
<mat-card>
<mat-card-header>
<img mat-card-avatar src="https://cdn.pixabay.com/photo/2018/10/11/12/31/black-cat-3739702_1280.jpg"
alt="My Photos">
<mat-card-title>Easy, Breezy, Beautiful</mat-card-title>
<mat-card-subtitle>Angular Material</mat-card-subtitle>
</mat-card-header>
<mat-card-content>
<p>
This is an amazing blog on Angular Material. Here is some awesome text about Angular Material.
Here is some awesome text about Angular Material. Here is some awesome text about Angular Material.
Here is some awesome text about Angular Material. Here is some awesome text about Angular Material.
Here is some awesome text about Angular Material. Here is some awesome text about Angular Material.
</p>
</mat-card-content>
<mat-card-actions align="end">
<button mat-raised-button color="primary">Read More</button>
</mat-card-actions>
</mat-card>
<div class="flex-container">
<div>
<button mat-raised-button color="primary">Raised Primary</button>
<button mat-raised-button color="accent">Raised Accent</button>
<button mat-stroked-button>Basic Stroked</button>
<button mat-stroked-button color="primary">Stroked Primary</button>
</div>
<button mat-fab>
<mat-icon>thumb_up</mat-icon>
</button>
<mat-radio-group aria-label="Select an option">
<mat-radio-button value="1">Option 1</mat-radio-button>
<mat-radio-button value="2">Option 2</mat-radio-button>
</mat-radio-group>
<mat-checkbox>Check me!</mat-checkbox>
<mat-form-field>
<mat-label>Choose a date</mat-label>
<input matInput [matDatepicker]="picker">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<mat-slider></mat-slider>
<button mat-flat-button color="primary" matTooltip="Info about the action" matTooltipPosition="after"
aria-label="Button that displays a tooltip when focused or hovered over">
Tooltip
</button>
</div>
<mat-paginator [length]="100" [pageSize]="10" [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
Congratulations! Your Angular app now includes Angular Material.
For a complete list of components and their usage, take a look at the Angular Material documentation.