Angular Material 8 Icons Tutorial with Real-world ExamplesπŸš€πŸš€πŸš€πŸš€πŸš€

Angular Material 8 Icons Tutorial with Real-world ExamplesπŸš€πŸš€πŸš€πŸš€πŸš€

Angular Material 8 Icons in Angular 8 app. - Learn to use Angular Material icons and also create custom svg icons with MatIconRegistry service...πŸ‘πŸ‘πŸ‘

Today I am going to share with you use Angular Material 8 Icons in Angular 8 app. Angular Material design is advantageous when it comes to building the user interface in no time. Nowadays, the usage of Angular material design is rapidly increasing. Angular material 8 offers lots of robust and beautiful UI components to build the user interface seamlessly. In this tutorial, I will teach how to use Angular Material 8 icons in your Angular 8 project.We’ll learn to use <mat-icon> material design component to display fonts icon and SVG icons. This material design ui component helps us to implement vector-based Angular material icons in angular 8 apps.

Angular Material 8 Icons Tutorial with Real-world Examples

To work with Angular material icons, first setup angular project and angular material ui library, follow the given below process.

Table of contents

  1. Install and setup Angular 8 project
  2. Install and setup Angular Material 8 ui library
  3. Create Custom SVG Icons with Angular Material 8 MatIconRegistry Service

#1 – Install and setup Angular 8 project

We have to install and setup angualr 8 project to display the Angular material 8 icons. Enter the following command.

ng new angular-material8-icons-tutorial

Answer the questions:

# ? Would you like to add Angular routing? = No
# ? Which stylesheet format would you like to use? = CSS

Go to the Angular 8 project folder.

cd angular-material8-icons-tutorial

#2 – Install and setup Angular Material 8 ui library

To set up angular material 8 ui library, enter the following command.

ng add @angular/material

Select the Angular material theme from the given options:

? Choose a prebuilt theme name, or "custom" for a custom theme: Indigo/Pink

❯ Indigo/Pink [ Preview: https://material.angular.io?theme=indigo-pink ] Deep Purple/Amber [ Preview: https://material.angular.io?theme=deeppurple-amber ] Pink/Blue Grey [ Preview: https://material.angular.io?theme=pink-bluegrey ] Purple/Green [ Preview: https://material.angular.io?theme=purple-green ]

It will ask for Hammer.js (Gesture recognition support) and Angular browser animationsupport.

Set up HammerJS for gesture recognition? (Y/n) = Y

? Set up browser animations for Angular Material? (Y/n) = Y

Importing MatIconModule in Separate Angular Material Module

We’ve installed the Angular material UI library in Angular project. I would suggest to create a specific angular-material.module.ts file to manage angular material components.

import { NgModule } from '@angular/core';
import { MatIconModule } from '@angular/material';

@NgModule({ imports: [ MatIconModule ], exports: [ MatIconModule ] })

export class AngularMaterialModule { }

Now we can can use angular material icons in our angular app. We just have to declare the <mat-icon> directive in our app to create the vector based material design icons.

Go to app.component.html file and include the code like given below.

<div style="text-align:center">
  <h1>
    <mat-icon>favorite_border</mat-icon> 
      Hello World 
    <mat-icon>favorite_border</mat-icon>
  </h1>
</div>

Visit app.module.ts file and import the AngularMaterialModule.

/* Angular material */
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AngularMaterialModule } from './material.module';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

@NgModule({ declarations: [...], imports: [ BrowserAnimationsModule, AngularMaterialModule, ], providers: [...], bootstrap: [...], schemas: [CUSTOM_ELEMENTS_SCHEMA] })

export class AppModule { }

To import the angular material theme, include the given below code to your src > index.html file.

<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Go to src > styles.css file and add the following code.

@import "[email protected]/material/prebuilt-themes/indigo-pink.css";

Now we are done with installing and setting up angular and angular material library. You can type the below command in terminal to run the angular project.

ng serve --open

Angular material 8 offers plenty of icons, to check out the full list of Angular material icons here.

#3 – Create Custom SVG Icons with Angular Material 8 MatIconRegistry Service

Suppose if you require to use custom svg icon in your angular 8 project. Keep your custom svg icon headphone.svg in your assets folder.

In order to work with custom icons with angular material <mat-icon> directive, we must import HttpClientModule in app.module.ts file.

import { HttpClientModule } from "@angular/common/http";

@NgModule({ imports: [ HttpClientModule ] })

export class AppModule {}

After that we are ready to register custom SVG icons with MatIconRegistry angular material service.

Go to app.component.html file and import the MatIconRegistry and place the icon registration service within your angular 8 component’s constructor method.

It takes 2 parameters, the 1st parameter is the icon’s label, and it should be a string type. Our 2nd parameter is the icons location path pointing towards the icon and its a SafeResourceUrl type. We need to import the DomSanitizer service to parse the url string path into SafeResourceUrl.

import { Component } from '@angular/core';
import { MatIconRegistry } from "@angular/material/icon";
import { DomSanitizer } from "@angular/platform-browser";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(
private matIconRegistry: MatIconRegistry,
private domSanitizer: DomSanitizer
) {
this.matIconRegistry.addSvgIcon(
"musicon",
this.domSanitizer.bypassSecurityTrustResourceUrl("../assets/headphone.svg")
);
}
}

Finally, we’ve setup the required services to create custom SVG icons within our Angular 8 app using Angular material 8 ui library.

The final step is to use the headphone.svg icon in app.component.html file.

<mat-icon svgIcon="musicon"></mat-icon>

Now you can display custom svg icon in your app using MatIconRegistry service.

ng serve --open

Angular Material 8 icons tutorial is completed now, i hope you must have loved it. Thanks for reading, have a good day!


Originally published by  Digamber Rawat at positronx.io

====================================================================

Follow me on Facebook | Twitter

Learn More


☞ Angular 8 (formerly Angular 2) - The Complete Guide

☞ Learn and Understand AngularJS

☞ The Complete Angular Course: Beginner to Advanced

☞ Angular Crash Course for Busy Developers

☞ Angular Essentials (Angular 2+ with TypeScript)

☞ Angular (Full App) with Angular Material, Angularfire & NgRx

☞ Angular & NodeJS - The MEAN Stack Guide

angular

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

Basics of Angular: Part-1

What is Angular? What it does? How we implement it in a project? So, here are some basics of angular to let you learn more about angular. Angular is a Typesc

Angular Sass: How To Use Sass In Angular 9 Tutorial

Sass in Angular is an extension of CSS that allows you toΒ use things like variables, nested rules, inline imports. Angular supports Sass, CSS, and Less.

How to Upgrade Angular 8 To Angular 9

Angular is currently the most popular JavaScript framework and is used by several expert developers when developing single-page applications or powerful web apps. It has become quite crucial for the developers of Angular to add more features to the framework and fix the bugs . Let's learn how to upgrade Angular 8 to Angular 9 using CLI.

Visualizing data with NGX-Charts in Angular

Data Science, Data Analytics, Big Data, these are the buzz words of today's world. A huge amount of data is being generated and analyzed every day. So communica