Skip to main content

Angular Material Tutorial

Components:

You will learn following components through this tutorial. You can click on the following components to jump to that section.

Toolbar

You can put headers, titles, or actions into one container using <mat-toolbar>.
Put following code into app.component.html file.
<div style="background: #eee8e8;">
  <mat-toolbar color="primary">
    <a style="color: white; text-decoration:none" target="_blank" href="https:www.devconquer.com">DevConquer</a>

    <button mat-icon-button>
      <mat-icon>account_circle</mat-icon>
    </button>
  </mat-toolbar>
</div>
You have to import MatToolbarModuleinto your app.module.ts file for proper working. I have created a separate module material.module.ts for importing and exporting angular material components and I imported that module into app.module.ts
Note: I have also imported MatIconModule and MatButtonModule as I have used a profile icon button.
See the code below.
import { NgModule } from '@angular/core';

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

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule
  ]
})
export class MaterialModule { }
Now you have to import MaterialModule into app.module.ts file.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MaterialModule } from './material.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MaterialModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Now run your project to see the output.
Output:
You can show floating panel with list of options using <mat-menu>.
Put following code into app.component.html file.
<div style="background: #eee8e8;">
  <mat-toolbar color="primary">
    <a style="color: white; text-decoration:none" target="_blank" href="https:www.devconquer.com">DevConquer</a>

    <button mat-icon-button [mat-menu-trigger-for]="menu">
      <mat-icon>account_circle</mat-icon>
    </button>
  </mat-toolbar>
  <mat-menu x-position="before" #menu="matMenu">
    <button mat-menu-item>Profile</button>
    <button mat-menu-item>Logout</button>
  </mat-menu>
</div>
You have to import MatMenuModule now into material.module.ts and export it. If you have not created a separate module then you only have to import MatMenuModule into app.module.ts
import { NgModule } from '@angular/core';

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

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatMenuModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatMenuModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.
Output:

Cards

<mat-card> is container where you can put text, images, videos, actions, etc.
Put following code into app.component.html file.
<mat-card style="max-width: 400px;">
    <h2>Eiffel Tower</h2>
    <img mat-card-image
      src="https://images.unsplash.com/photo-1502602898657-3e91760cbb34?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=80"
      alt="">
    <mat-card-content>
      The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the
      engineer Gustave Eiffel, whose company designed and built the tower.
    </mat-card-content>

    <mat-card-actions>
      <button mat-button>
        <mat-icon>thumb_up</mat-icon>
        Like
      </button>
      <button mat-button>
        <mat-icon>share</mat-icon>Share
      </button>
    </mat-card-actions>
  </mat-card>
You have to import MatCardModule now into material.module.ts and export it. If you have not created a separate module then you only have to import MatCardModule into app.module.ts
import { NgModule } from '@angular/core';

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

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.
Output:

List

You can create list of items using <mat-list>.
Put following code into app.component.html file.
<mat-list>
  <mat-list-item role="listitem">List Item 1</mat-list-item>
  <mat-list-item role="listitem">List Item 2</mat-list-item>
  <mat-list-item role="listitem">List Item 3</mat-list-item>
</mat-list>
You have to import MatListModule now into material.module.ts and export it. If you have not created a separate module then you only have to import MatListModule into app.module.ts
import { NgModule } from '@angular/core';

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

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.

Divider

You can use for styling of a line separator
Put following code into app.component.html file.
<mat-list>
    <mat-list-item>List Item 1</mat-list-item>
    <mat-divider></mat-divider>
    <mat-list-item>List Item 2</mat-list-item>
    <mat-divider></mat-divider>
    <mat-list-item>List Item 3</mat-list-item>
  </mat-list>
You have to import MatDividerModulenow into material.module.ts and export it. If you have not created a separate module then you only have to import MatDividerModule into app.module.tsfile.
import { NgModule } from '@angular/core';

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

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.
Output:

Grid List

You can use for arranging cells into grid-based layout.
Put following code into app.component.html file.
  <mat-grid-list cols="2" rowHeight="2:1">
    <mat-grid-tile style="background: skyblue;">1</mat-grid-tile>
    <mat-grid-tile style="background: grey;">2</mat-grid-tile>
    <mat-grid-tile style="background: skyblue;">3</mat-grid-tile>
    <mat-grid-tile style="background: grey;">4</mat-grid-tile>
  </mat-grid-list>
You have to import MatGridListModulenow into material.module.ts and export it. If you have not created a separate module then you only have to import MatGridListModule into app.module.tsfile.
import { NgModule } from '@angular/core';

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

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.

Tabs

You can show your content into separate view using tabs. You can give names for tabs by setting labels. Active tab is designed with animated ink bar by default. Also animation is applied by default when user clicks from one tab to another.
Put following code into app.component.html file.
<mat-tab-group>
    <mat-tab label="First"> Content for tab 1 </mat-tab>
    <mat-tab label="Second"> Content for tab 2 </mat-tab>
    <mat-tab label="Third"> Content for tab 3 </mat-tab>
  </mat-tab-group>
You have to import MatTabsModule now into material.module.ts and export it. If you have not created a separate module then you only have to import MatTabsModule into app.module.ts file.
import { NgModule } from '@angular/core';

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

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.

Buttons

Buttons are important for user actions. Many types of buttons are provided by angular material. They are BasicRaisedStrokedFlatIconFab, and Mini Fabbuttons.
Put following code into app.component.file and run your project to see the output.
<mat-card style="display: flex; justify-content: space-around;">
    <button mat-button color="primary">Primary</button>
    <button mat-raised-button color="primary">Primary</button>
    <button mat-stroked-button color="primary">Primary</button>
    <button mat-flat-button color="primary">Primary</button>
    <button mat-icon-button color="primary">
      <mat-icon>favorite</mat-icon>
    </button>
    <button mat-fab color="primary">Primary</button>
    <button mat-mini-fab color="primary">Primary</button>
  </mat-card>


  <div>
    <h3 style="text-align: center;">Action buttons</h3>
    <mat-card style="display: flex; justify-content: space-around;">
      <button mat-raised-button color="warn">Cancel</button>
      <button mat-raised-button color="primary">Save</button>
      <button mat-raised-button color="accent">Delete</button>
    </mat-card>
  </div>
You have to import MatButtonModulenow into material.module.ts and export it. If you have not created a separate module then you only have to import MatButtonModule into app.module.tsfile.
I have already imported MatButtonModule at the start.
Output:

Badge

A badge is a UI element used as a notification status description. A badge is a small circle usually containing a number or other set of characters to show how many notifications are present.
Put following code into app.component.html file.
<div style="text-align: center;">
  <p>
    <span matBadge="2" matBadgeOverlap="false">Text</span>
  </p>
  <p>
    <button mat-raised-button color="primary" matBadge="5" matBadgePosition="before" matBadgeColor="accent">
      Action Button
    </button>
  </p>
  <p>
    <mat-icon matBadge="10" matBadgeColor="warn">home</mat-icon>
  </p>
</div>
You have to import MatBadgeModule now into material.module.ts and export it. If you have not created a separate module then you only have to import MatBadgeModule into app.module.ts file.
import { NgModule } from '@angular/core';

import {
  MatToolbarModule,
  MatIconModule,
  MatButtonModule,
  MatMenuModule,
  MatCardModule,
  MatListModule,
  MatDividerModule,
  MatGridListModule,
  MatTabsModule,
  MatBadgeModule
} from '@angular/material';

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule,
    MatBadgeModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule,
    MatBadgeModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.
Output:
Position:
Badge position can be changed using property matBadgePosition with giving value like above|below and before|after. By default position is above after.
Color:
You can change the color of badges using matBadgeColor property. Using matBadgeColor property you can then give colors like a primary, accent or warn.

Icons

You can use vector-based icons easily into you app using mat-icon. mat-icon supports both SVG icons and icon fonts and, but not bitmap-based formats like png, jpg, etc.
Put following code into app.component.file and run your project to see the output.
 <div>
    <h3 style="text-align: center;">Icons</h3>
    <mat-card style="display: flex; justify-content: space-around;">
      <mat-icon>home</mat-icon>
      <mat-icon>dashboard</mat-icon>
      <mat-icon>settings_applications</mat-icon>
      <mat-icon>account_circle</mat-icon>
      <mat-icon>notifications</mat-icon>
      <mat-icon>refresh</mat-icon>
    </mat-card>
  </div>
You have to import MatIconModule now into material.module.ts and export it. If you have not created a separate module then you only have to import MatIconModule into app.module.ts file.
I have already imported MatIconModuleat the start.
Output:

SnackBar

You can use MatSnackBar as a service for displaying snack-bar notifications.
Put following code into app.component.html file.
<div style="text-align: center;">
    <h2>Snackbar</h2>
    <button mat-raised-button color="primary" (click)="showSnackBar()">Show snack-bar</button>
  </div>
Put following code into app.component.tsfile.
import { Component } from '@angular/core';
import { MatSnackBar } from '@angular/material';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular-material-part-II';
  constructor(private snackBar: MatSnackBar) { }

  showSnackBar(message: string, action: string) {
    this.snackBar.open('This is snackBar.', '', { duration: 1000 });
  }
}
You have to import MatSnackBarModulenow into material.module.ts and export it. If you have not created a separate module then you only have to import MatSnackBarModule into app.module.tsfile.
import { NgModule } from '@angular/core';

import {
  MatToolbarModule,
  MatIconModule,
  MatButtonModule,
  MatMenuModule,
  MatCardModule,
  MatListModule,
  MatDividerModule,
  MatGridListModule,
  MatTabsModule,
  MatBadgeModule,
  MatProgressSpinnerModule,
  MatProgressBarModule,
  MatSnackBarModule
} from '@angular/material';

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule,
    MatBadgeModule,
    MatProgressSpinnerModule,
    MatProgressBarModule,
    MatSnackBarModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule,
    MatBadgeModule,
    MatProgressSpinnerModule,
    MatProgressBarModule,
    MatSnackBarModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.

Tooltip

You can use tooltip component to show extra information when user hovers on text label, icon, etc.
Put following code into app.component.html file.
<div style="text-align: center;">
    <mat-icon matTooltip="Extra information">
      help
    </mat-icon>
  </div>
You have to import MatTooltipModulenow into material.module.ts and export it. If you have not created a separate module then you only have to import MatTooltipModule into app.module.tsfile.
import { NgModule } from '@angular/core';

import {
  MatToolbarModule,
  MatIconModule,
  MatButtonModule,
  MatMenuModule,
  MatCardModule,
  MatListModule,
  MatDividerModule,
  MatGridListModule,
  MatTabsModule,
  MatBadgeModule,
  MatProgressSpinnerModule,
  MatProgressBarModule,
  MatSnackBarModule,
  MatTooltipModule
} from '@angular/material';

@NgModule({
  imports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule,
    MatBadgeModule,
    MatProgressSpinnerModule,
    MatProgressBarModule,
    MatSnackBarModule,
    MatTooltipModule
  ],
  exports: [
    MatToolbarModule,
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatCardModule,
    MatListModule,
    MatDividerModule,
    MatGridListModule,
    MatTabsModule,
    MatBadgeModule,
    MatProgressSpinnerModule,
    MatProgressBarModule,
    MatSnackBarModule,
    MatTooltipModule
  ]
})
export class MaterialModule { }
Now run your project to see the output.
Output:

Getting Problems ?

If you are getting any problems when implementing components from this angular material tutorial, ask me by commenting.

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...