Tutorial

Getting Started With Angular Material 2

Published on October 2, 2016
Default avatar

By Alligator.io

Getting Started With Angular Material 2

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Angular Material 2 brings Material Design components to Angular 2+ apps. The goal of the project is to build a full array of components to make it very easy to built Material Design interfaces for mobile and desktop.

The latest release of Angular Material depends on Angular 4+

Here’s how to get started with Angular Material 2:

1. npm install angular-material & hammerjs

First install Angular Material, Angular animations, and Hammer.js in your project with these commands:

  1. npm install --save @angular/material @angular/animations @angular/cdk
  1. npm install --save hammerjs

Hammer.js is an optional dependency and helps with touch support for a few of the components.

2. angular-cli.json

If you decide to use Hammer.js, and given that you’ve started your project with the Angular CLI, modify your angular-cli.json file to add the Hammer.js library. Look for the Json *“scripts”* array and add the following path for hammer.js:

angular-cli.json
"scripts": [
  "../node_modules/hammerjs/hammer.min.js"
],

You may need to restart your local server for the changes to angular-cli.json to take effect.

3. Custom Material Module

Prior to Angular Material 2 Beta 3, there was a global MaterialModule that could be imported in the app module to make the components available. The downside to that is that tree-shaking is not efficient enough to remove all the unused code.

MaterialModule has therefore been deprecated in favor of defining a project-specific custom material module where you import and export only the needed components. Here’s what our module can look like:

material.module.ts
import { NgModule } from '@angular/core';

import {
  MatButtonModule,
  MatMenuModule,
  MatToolbarModule,
  MatIconModule,
  MatCardModule
} from '@angular/material';

@NgModule({
  imports: [
    MatButtonModule,
    MatMenuModule,
    MatToolbarModule,
    MatIconModule,
    MatCardModule
  ],
  exports: [
    MatButtonModule,
    MatMenuModule,
    MatToolbarModule,
    MatIconModule,
    MatCardModule
  ]
})
export class MaterialModule {}

You’ll then import this module in the root app module.

4. Add Angular Material to your app module

Import MaterialModule and add it to your imports. You’ll also need to import the necessary for animations in your module. Your app module (e.g.: app.module.ts) will look a little bit like this:

app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MaterialModule } from './material.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MaterialModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

5. Import a pre-built theme and Material icons

There are a few pre-built themes installed automatically with Angular Material. These set the colors and basic styling. The available themes are: indigo-pink, deeppurple-amber, purple-green and pink-bluegrey.

To import a theme, you can add something like this to your global styles.css file:

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

You can also have access to the Material Design icons and use named icons with the <mat-icon> component. To import them to your project, you can add this to the head section of your project’s root index.html file:

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

6. Angular Material is ready!

It’s now ready for you to start using the available Angular Material components in your templates. Here’s for example some markup for a template of a sample app:

<div>
  <mat-toolbar color="primary">
    <span><mat-icon>mood</mat-icon></span>

    <span>Yay, Material in Angular 2!</span>

    <button mat-icon-button [mat-menu-trigger-for]="menu">
      <mat-icon>more_vert</mat-icon>
    </button>
  </mat-toolbar>
  <mat-menu x-position="before" #menu="matMenu">
    <button mat-menu-item>Option 1</button>
    <button mat-menu-item>Option 2</button>
  </mat-menu>

  <mat-card>
    <button mat-button>All</button>
    <button mat-raised-button>Of</button>
    <button mat-raised-button color="primary">The</button>
    <button mat-raised-button color="accent">Buttons</button>
  </mat-card>

  <span class="done">
    <button mat-fab>
      <mat-icon>check circle</mat-icon>
    </button>
  </span>
</div>

And to this we added only the following CSS to our global styles.css:

styles.css
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;
}

And here’s the look of our sample app:

Our sample Material Design app

Learning More

Here are a few more posts to help you get started:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel