Angular Material Design Components With Reactive Forms - Part One

Introduction

Today, in this article, I'm going to show you how to use Angular Material Design Component in your Angular app. Before that, you should know some basic structure of Angular app and its related functionality. In this part, we are going to see how to implement Reactive Forms with Angular 5.

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

To check latest version of NPM,
  •  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.

  1. Ng serve  
Now, go to localhost:4200. Your app is running there.

Step 3

So, let’s get started by configuring our app for Material design. Execute the following commands to install Angular Material.

  1. Npm install –-save –g @angular/material
  2. Npm install –-save –g @angular/cdk
  3. Npm install –-save –g @angular/animations

As you can see, I have installed 3 main dependencies - material, its cdk, and its 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.

  1. import { BrowserModule } from '@angular/platform-browser';      
  2. import { NgModule } from '@angular/core';      
  3. import { FormsModule,ReactiveFormsModule } from '@angular/forms';      
  4. import {      
  5.   MatButtonModule,      
  6.   MatMenuModule,      
  7.   MatToolbarModule,      
  8.   MatIconModule,      
  9.   MatCardModule,      
  10.   MatFormFieldModule,      
  11.   MatInputModule,      
  12.   MatDatepickerModule,      
  13.   MatDatepicker,      
  14.   MatNativeDateModule,      
  15.   MatRadioModule,      
  16.   MatSelectModule,      
  17.   MatOptionModule,      
  18.   MatSlideToggleModule,ErrorStateMatcher,ShowOnDirtyErrorStateMatcher      
  19. } from '@angular/material';      
  20. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';      
  21. import { AppComponent } from './app.component';      
  22.       
  23. @NgModule({      
  24.   declarations: [      
  25.     AppComponent      
  26.   ],      
  27.   imports: [      
  28.     BrowserModule,      
  29.     FormsModule,      
  30.     ReactiveFormsModule,      
  31.     MatButtonModule,      
  32.     MatMenuModule,      
  33.     MatToolbarModule,      
  34.     MatIconModule,      
  35.     MatCardModule,      
  36.     BrowserAnimationsModule,      
  37.     MatFormFieldModule,      
  38.     MatInputModule,      
  39.     MatDatepickerModule,      
  40.     MatNativeDateModule,      
  41.     MatRadioModule,      
  42.     MatSelectModule,      
  43.     MatOptionModule,      
  44.     MatSlideToggleModule      
  45.   ],      
  46.   exports: [      
  47.     MatButtonModule,      
  48.     MatMenuModule,      
  49.     MatToolbarModule,      
  50.     MatIconModule,      
  51.     MatCardModule,      
  52.     BrowserAnimationsModule,      
  53.     MatFormFieldModule,      
  54.     MatInputModule,      
  55.     MatDatepickerModule,      
  56.     MatNativeDateModule,      
  57.     MatRadioModule,      
  58.     MatSelectModule,      
  59.     MatOptionModule,      
  60.     MatSlideToggleModule      
  61.   ],      
  62.   providers: [      
  63.     {provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher}      
  64.   ],      
  65.   bootstrap: [AppComponent]      
  66. })      
  67. 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.

As you can see, I have imported the browser animation module as well as we should also import all the components that we want to use in our project.
  • 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.

  1. Indigo-pink.css
  2. Pink-bluegrey.css
  3. Deeppurple-amber.css [I'm using this one]
  4. Purple-green.css

Following is my styles.css [root src directory] code.

  1. @import '~https://fonts.googleapis.com/icon?family=Material+Icons';    
  2. @import '~@angular/material/prebuilt-themes/deeppurple-amber.css';    
  3. .mat-card {    
  4.     text-align: -webkit-center;    
  5. }    
  6.     
  7. .demo-toolbar {    
  8.     display: flex;    
  9.     align-items: center;    
  10.     width100%;    
  11. }    
  12.     
  13. .demo-form {    
  14.     min-width150px;    
  15.     max-width500px;    
  16.     width100%;    
  17. }    
  18.     
  19. .demo-full-width {    
  20.     width100%;    
  21. }    
  22.     
  23. body {    
  24.     margin0;    
  25.     font-family: Roboto, sans-serif;    
  26. }    
  27.     
  28. mat-card {    
  29.     max-width80%;    
  30.     margin2em auto;    
  31.     text-aligncenter;    
  32. }    
  33.     
  34. mat-toolbar-row {    
  35.     justify-content: space-between;    
  36. }    
  37.     
  38. .done {    
  39.     positionfixed;    
  40.     bottom: 20px;    
  41.     right: 20px;    
  42.     colorwhite;    
  43. }    
  44.     
  45. .content-center {    
  46.     text-align: -webkit-center;    
  47. }    

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,

  1. Npm install --save hammerjs

So, hammer.js will be installed on your machine successfully.

Do not forget to import it into main.ts file [Actually, it is the main entry point of Angular APP].

Like - import ‘hammerjs’;

Step 7

So, let's create a simple form using Angular Material Components. Find the below snippets for the same that I've created [app.component.html].
  1. <!-- Main Toolbar of an App -->    
  2. <mat-toolbar color="accent">    
  3.     <span>Manav Pandya - C#Corner</span>    
  4.     <span class="demo-toolbar"></span>    
  5.     <button mat-button href="asp-dotnet-mvc-tutorials.blogspot.in">Go To My Blog</button>    
  6.     
  7. </mat-toolbar>    
  8.     
  9. <mat-card>    
  10.     <!-- Title of an Card -->    
  11.     <mat-card-title>    
  12.         Angular Material Component With Angular 5    
  13.     </mat-card-title>    
  14.     
  15.     <mat-card-content>    
  16.         <form>    
  17.             <table>    
  18.                 <tr>    
  19.                     <td>    
  20.                         <mat-form-field class="demo-full-width">    
  21.                             <input matInput placeholder="First Name">    
  22.                         </mat-form-field>    
  23.                     </td>    
  24.                     <td>    
  25.                         <mat-form-field class="demo-full-width">    
  26.                             <input matInput placeholder="Last Name">    
  27.                         </mat-form-field>    
  28.                     </td>    
  29.                 </tr>    
  30.                 <tr>    
  31.                     <td colspan="2">    
  32.                         <mat-form-field class="demo-full-width">    
  33.                             <textarea matInput placeholder="Address" matTextareaAutosize matAutosizeMinRows="2" matAutosizeMaxRows="5"></textarea>    
  34.                         </mat-form-field>    
  35.                     </td>    
  36.                 </tr>    
  37.                 <tr>    
  38.                     <td colspan="2">    
  39.                         <mat-form-field class="demo-full-width">    
  40.                             <input matInput [matDatepicker]="picker" placeholder="Date of birth">    
  41.                             <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>    
  42.                             <mat-datepicker touchUi="true" #picker></mat-datepicker>    
  43.                         </mat-form-field>    
  44.                     </td>    
  45.                 </tr>    
  46.                 <tr>    
  47.                     <td>    
  48.                         <span>Gender</span><br><br>    
  49.                         <mat-radio-group>    
  50.                             <mat-radio-button value="1">Male</mat-radio-button>    
  51.                             <mat-radio-button value="2">Female</mat-radio-button>    
  52.                         </mat-radio-group>    
  53.                     </td>    
  54.                     <td><br>    
  55.                         <mat-form-field>    
  56.                             <mat-select placeholder="Select Blog" [(value)]="selected">    
  57.                                 <mat-option>-- Select Any --</mat-option>    
  58.                                 <mat-option value="1">C#Corner</mat-option>    
  59.                                 <mat-option value="2">C#Corner</mat-option>    
  60.                                 <mat-option value="3">C#Corner</mat-option>    
  61.                             </mat-select>    
  62.                         </mat-form-field>    
  63.                     </td>    
  64.                 </tr>    
  65.                 <tr>    
  66.                     <td colspan="2">    
  67.                         <mat-form-field class="demo-full-width">    
  68.                             <input matInput placeholder="Email">    
  69.                         </mat-form-field>    
  70.                     </td>    
  71.                 </tr>    
  72.                 <tr>    
  73.                     <td colspan="2">    
  74.     
  75.                     </td>    
  76.                 </tr>    
  77.                 <tr>    
  78.                     <td colspan="2" class="content-center">    
  79.                         <button mat-raised-button color="accent">Submit</button>    
  80.                         <button mat-raised-button color="accent">Clear</button>    
  81.                     </td>    
  82.                 </tr>    
  83.             </table>    
  84.         </form>    
  85.     </mat-card-content>    
  86. </mat-card>   
Code explanation
  • <mat-form-field>
    This tag of Material component is container of html control , with the help of this we can define theme of an control, Like : accent , primary , warn etc .

  • <mat-toolbar>
    In this part of the form, I have used Angular material toolbar module, it contains the title of the toolbar and a material button.

  • <mat-card>
    In this section our actual form is contained, where I have used most of the Material Components.

  • <mat-card-title>
    Provides the title of the card for display purpose.

  • <mat-card-content>
    It contains actual content of an card element. And other controls as well like,
    • <mat-radio-button> - For using radio button option
    • <mat-datepicker> -To use Datepicker
    • <mat-select> - To use Dropdown list of values
    • <mat-option> - To provide different values to <mat-select>
Step 8

So far we have created a complete form using Angular material components

Let's try and run following command :
  1. ng serve -o
And your application is running on your browser. You can see the following output.



As you can see, the above form looks good but till now, it's just an HTML form, and on submitting the form, nothing is going to happen.

Conclusion

So far, we have learned how to install and configure Angular Material Component and configure it. At last, we have created a basic form with many controls within it. In my upcoming article, we will learn the following.
  • Reactive Form Introduction
  • Implementation Of Reactive Form
  • Validations & Form Submission
Stay tuned for Part-2.