Angular 5 With Material Design Part One: Integrating Angular Material

Introduction

In this article, we'll walk through the steps which are needed to set up an Angular project including the Angular Material Design Library in your project.

We're going to use the Angular CLI to start this project and I'm assuming you already have it installed. If you haven't, use the following command to install it globally.

npm install -g @angular/cli

Step 1: Create a project

Use the following command to create a new Angular project.

ng new MyNgAppWithMaterial

E.g.

Angular

Note

I am using my “D” Drive to save my project. You can select any path where you want to create the project.

It will create a new project. After the project is successfully created, go to the Angular Project folder by changing the directory

cd MyNgAppWithMaterial

To check if everything is working correctly, we will run the project using the following command.

ng serve –o

E.g.

Angular

This command builds the application and opens it in our default browser.

To stop the server, press CTRL + C while you are on the command prompt and then "Y" and ENTER key. This will stop the server.

Step 2: Install Angular Material and Angular CDK

Type the following command to install the dependencies.

npm install --save @angular/material @angular/cdk

E.g.

Angular

Step 3: Install Animations

Some material components depend on the Angular animations module in order to be able to do more advanced transitions. If you want these animations to work in your app, you have to install the @angular/animations module and include the BrowserAnimationsModule in your app.

Command

npm install --save @angular/animations

E.g.

Angular

We need to import the Angular animation module into app.module.ts file.

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. // import the angular animation module  
  4. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  5.   
  6.   
  7. import { AppComponent } from './app.component';  
  8.   
  9.   
  10. @NgModule({  
  11.   declarations: [  
  12.     AppComponent  
  13.   ],  
  14.   imports: [  
  15.     BrowserModule, BrowserAnimationsModule  
  16.   ],  
  17.   providers: [],  
  18.   bootstrap: [AppComponent]  
  19. })  
  20. export class AppModule { }  

Step 4: Import the component modules

Next, for each material component we want to use in our project, we have to import it into the imports array of ngModule above.

If you only have a couple of components, you could import them directly into this file. However, for the purposes of organization and keeping your AppModule brief, you can create a custom module specifically for your material component imports. We'll do that.

To create a new module, use the following command.

Command

ng generate module material Or ng g m material

E.g.

Angular

-m=app.module option is used to register material to app.module or you can register it manually like below.

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. // import the angular animation module  
  4. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  5.   
  6.   
  7. import { AppComponent } from './app.component';  
  8. import { MaterialModule } from './material/material.module';  
  9.   
  10.   
  11. @NgModule({  
  12.   declarations: [  
  13.     AppComponent  
  14.   ],  
  15.   imports: [  
  16.     BrowserModule, BrowserAnimationsModule, MaterialModule  
  17.   ],  
  18.   providers: [],  
  19.   bootstrap: [AppComponent]  
  20. })  
  21. export class AppModule { }   

Include all your required material component module, we're only including the MatInputModule like below in material.module

  1. import { NgModule } from '@angular/core';  
  2. import { CommonModule } from '@angular/common';  
  3. import {MatInputModule} from '@angular/material/input';  
  4.   
  5. @NgModule({  
  6.   imports: [  
  7.     CommonModule,  
  8.     MatInputModule  
  9.   ],  
  10.   exports: [MatInputModule],  
  11.   declarations: []  
  12. })  
  13. export class MaterialModule { }  

Step 5: Include a theme

Including a theme is required to apply all of the core and theme styles to your application.

Since we're using the Angular CLI, we can add this to our styles.css:

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

E.g.

  1. /* You can add global styles to this file, and also import other style files */  
  2. @import "~@angular/material/prebuilt-themes/indigo-pink.css";  

Step 6: Gesture Support

Some components (mat-slide-toggle, mat-slider, matTooltip) rely on HammerJS for gestures. To get the full feature set of these elements, HammerJS must be loaded into the application.

We can add HammerJS to our application using following command.

Command

npm install --save hammerjs

E.g.

Angular

After installing, import it into your app’s entry point (e.g. src/main.ts).

  1. import { enableProdMode } from '@angular/core';  
  2. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';  
  3.   
  4. import { AppModule } from './app/app.module';  
  5. import { environment } from './environments/environment';  
  6.   
  7. import 'hammerjs';  
  8.   
  9. if (environment.production) {  
  10.   enableProdMode();  
  11. }  
  12.   
  13. platformBrowserDynamic().bootstrapModule(AppModule)  
  14.   .catch(err => console.log(err));  

Now, we're going to use any Material Design Icons (they are used quite frequently throughout material), we need to import Material Icons in the /src/index.html file between the head tags.

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>MyNgAppWithMaterial</title>  
  6.   <base href="/">  
  7.   
  8.   <meta name="viewport" content="width=device-width, initial-scale=1">  
  9.   <link rel="icon" type="image/x-icon" href="favicon.ico">  
  10.   <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">  
  11. </head>  
  12. <body>  
  13.   <app-root></app-root>  
  14. </body>  
  15. </html>  

Step 6 - Using Angular material component in our component

Okay, now everything is included correctly. Just write some essential Material component inside the app.component.html file.

Add following to the /src/app/app.component.html template,

  1. <form class="example-form">  
  2.   <mat-form-field class="example-full-width">  
  3.     <input matInput placeholder="Name" value="Dilip">  
  4.   </mat-form-field>  
  5.   
  6.   <mat-form-field class="example-full-width">  
  7.     <textarea matInput placeholder="Leave a comment"></textarea>  
  8.   </mat-form-field>  
  9. </form>  

The CSS tab has a couple of rule sets for the above example to fully work. Visit the /src/app/app.component.css file and paste the following,

  1. .example-form {  
  2.   min-width150px;  
  3.   max-width500px;  
  4.   width100%;  
  5. }  
  6.   
  7. .example-full-width {  
  8.   width100%;  
  9. }  

Now, save and run your project using command ng serve –o

Then, you can see the two animated textboxes.
 
Angular

And that's the process of integrating and using material components in Angular 5.

Summary

In this article, I discussed how we can include the Angular Material Design Library into our angular project. In the next article, we will start Angular CRUD with Material Design.