Introduction
Let’s start understanding how Angular Material works.
Step 1
Let’s create a new app using the following commands.
Note
Please make sure that you have the latest Node.js version already installed , and if not, go through the following link to download the latest version. Click Here To Download Latest Version Of Node.js
To check its versions, try the following command
- ng –v
- node –v
- npm –v
Now, create a new directory and our app inside it.
- Ng new angularmaterialform
And then, open it to VS Code editor by writing the following command.
- Code
And you can see that the following structure is created.
Step 2
Now, you have the app created with you. Serve it using following command.
- Ng serve
Step 3
So, let’s get started by configuring our app for Material design. Execute the following commands to install Angular Material.
- Npm install –-save –g @angular/material
- Npm install –-save –g @angular/cdk
- Npm install –-save –g @angular/animations
Note
We have installed animations because some of the Angular Material components are based on animation, like - swipe , zoom-in , zoom out etc .
So now, we have completely configured our app.
Step 4
In the next step, we use imports in our main module file [installed packages] , so that we can use it globally.
Here, I have pasted my module.ts file code.
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { FormsModule,ReactiveFormsModule } from '@angular/forms';
- import {
- MatButtonModule,
- MatMenuModule,
- MatToolbarModule,
- MatIconModule,
- MatCardModule,
- MatFormFieldModule,
- MatInputModule,
- MatDatepickerModule,
- MatDatepicker,
- MatNativeDateModule,
- MatRadioModule,
- MatSelectModule,
- MatOptionModule,
- MatSlideToggleModule,ErrorStateMatcher,ShowOnDirtyErrorStateMatcher
- } from '@angular/material';
- import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
- import { AppComponent } from './app.component';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- FormsModule,
- ReactiveFormsModule,
- MatButtonModule,
- MatMenuModule,
- MatToolbarModule,
- MatIconModule,
- MatCardModule,
- BrowserAnimationsModule,
- MatFormFieldModule,
- MatInputModule,
- MatDatepickerModule,
- MatNativeDateModule,
- MatRadioModule,
- MatSelectModule,
- MatOptionModule,
- MatSlideToggleModule
- ],
- exports: [
- MatButtonModule,
- MatMenuModule,
- MatToolbarModule,
- MatIconModule,
- MatCardModule,
- BrowserAnimationsModule,
- MatFormFieldModule,
- MatInputModule,
- MatDatepickerModule,
- MatNativeDateModule,
- MatRadioModule,
- MatSelectModule,
- MatOptionModule,
- MatSlideToggleModule
- ],
- providers: [
- {provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher}
- ],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Note
It is a best practice to create a separate module file for all Material Components that you are going to use.
- MatButtonModule
- MatMenuModule
- MatToolbarModule
- MatIconModule
- MatCardModule
- MatFormFieldModule
- MatInputModule
- MatDatepickerModule
- MatDatepicker
- MatNativeDateModule
- MatRadioModule
- MatSelectModule
- MatOptionModule
- MatSlideToggleModule
So far, we have set up module.ts file with necessary components.
Step 5
Next step is to provide some look and feel to our Material Design app theme.
There are some pre-defined themes available. You can choose anyone from below.
- Indigo-pink.css
- Pink-bluegrey.css
- Deeppurple-amber.css [I'm using this one]
- Purple-green.css
Following is my styles.css [root src directory] code.
- @import '~https://fonts.googleapis.com/icon?family=Material+Icons';
- @import '~@angular/material/prebuilt-themes/deeppurple-amber.css';
- .mat-card {
- text-align: -webkit-center;
- }
- .demo-toolbar {
- display: flex;
- align-items: center;
- width: 100%;
- }
- .demo-form {
- min-width: 150px;
- max-width: 500px;
- width: 100%;
- }
- .demo-full-width {
- width: 100%;
- }
- body {
- margin: 0;
- font-family: Roboto, sans-serif;
- }
- mat-card {
- max-width: 80%;
- margin: 2em auto;
- text-align: center;
- }
- mat-toolbar-row {
- justify-content: space-between;
- }
- .done {
- position: fixed;
- bottom: 20px;
- right: 20px;
- color: white;
- }
- .content-center {
- text-align: -webkit-center;
- }
Step 6
In addition to this, we can add gesture support to our app using HammerJS.
Why we need HammerJS ?
HammerJS is used mainly for adding gesture kind of effects support, like - Swipe, Zoom, rotate.
Execute the following command,
- Npm install --save hammerjs
So, hammer.js will be installed on your machine successfully.
Like - import ‘hammerjs’;
Step 7


Shariq SaadPosted Oct 6, 2018, 8:54 AM
Can we resize mat-datepicker display size. Also can we change its font size and reduce padding
Ken PerregauxPosted Jul 16, 2018, 11:10 AM
Great example Manav, however, my styling does not look the same as yours. The placeholder is not light grey and there is no underline. Did I forget a step or am I missing some CSS?
sai kumarPosted Jun 24, 2018, 5:53 AM
Hai this seems nice tutorial i want to ask you a question i generated myprofile component and i placed your html and css but each textbox seems to be very small large screen.so i want to increase width and increase padding of side by side textbox's .Can anyone help with this.
Ben HalversonPosted Jun 1, 2018, 3:41 PM
You dont need -g to install npm dependences. This is the more common way of installing npm modules `npm install -S @angular/cdk @angular/material @angular/animations`
Mohammad MalikPosted Mar 16, 2018, 6:59 AM
Thank you. Very helpful.
Tridip BhattacharjeePosted Mar 14, 2018, 3:24 AM
What is the meaning of Reactive Form...please share some background. thanks
Bikash SahuPosted Feb 26, 2018, 10:46 PM
Good example.well done!
RakeshPosted Jan 10, 2018, 2:12 PM
Good one...........
Manav PandyaPosted Jan 10, 2018, 4:50 AM
Thanks Mangesh Gaherwar sir ........
Mangesh GPosted Jan 10, 2018, 4:11 AM
good one manav !!!! well done
Manav PandyaPosted Jan 10, 2018, 3:19 AM
Glad you liked it Sir Akshay Arora ....
Akshay AroraPosted Jan 10, 2018, 3:12 AM
This is a Nice one . .
Dharmraj ThakurPosted Jan 10, 2018, 12:07 AM
Awesome manav sir... well done
Manav PandyaPosted Jan 9, 2018, 11:39 PM
Thanks sir Hiten Pandya ........
Hiten PandyaPosted Jan 9, 2018, 11:31 PM
Good one Manav.... Thanks for this simple explanation
Manav PandyaPosted Jan 9, 2018, 11:27 PM
Glad you liked it !!!!! Naresh Singhal
Naresh SinghalPosted Jan 9, 2018, 11:25 PM
Very good and explained deeply with good example...Sir Manav Pandya