Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly register font-awesome for md-icon?

The documentation of md-icon describes how to use font-awesome in several parts and suggests we could use font-awesome eventually like

<md-icon fontSet="fa" fontIcon="alarm"></md-icon> 

But the documentation is very confusing and I can hardly find a routine to register 3rd font set like font-awesome for md-icon via Google.

Any help is appreciated!

PS: I know the normal <i> way generally works but it doesn't seem working in some examples, where the md-icon is used originally.

like image 608
Haoliang Yu Avatar asked May 07 '17 21:05

Haoliang Yu


People also ask

How do I use Font Awesome for icons?

You can place Font Awesome icons just about anywhere using the CSS Prefix fa and the icon's name. Font Awesome is designed to be used with inline elements (we like the <i> tag for brevity, but using a <span> is more semantically correct). icon If you change the font-size of the icon's container, the icon gets bigger.

How do you reference Font Awesome icons in HTML?

Add Icons to HTML We recommend using <i> element with the Font Awesome CSS classes for the style class for the style of icon you want to use and the icon name class with the fa- prefix for the icon you want to use.

Why some of the Font Awesome icons does not show?

Are you using Font Awesome Free or Pro? - Some icons are only available in Font Awesome Pro. Double-check that the icon you want is in the version of Font Awesome you're referencing and using. Also, make sure you are using and referencing the right style prefix and supporting files to use Pro icons.


1 Answers

In your AppModule add:

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

Then add MatIconModule to your imports e.g.:

imports: [...    MatIconModule ...] 

Add MatIconRegistry to your providers:

providers: [...     MatIconRegistry ....] 

Then add the following to your AppModule class:

export class AppModule {     constructor(         ...public matIconRegistry: MatIconRegistry) {         matIconRegistry.registerFontClassAlias('fontawesome', 'fa');     } 

Then you can use anywhere in your project like so:

<mat-icon fontSet="fa" fontIcon="fa-times-circle"></mat-icon> 

Update

You'll need to include fontawesome in your project somewhere. I use angular-cli so adding the font-awesome npm package:

npm install font-awesome --save 

and including it in styles list in angular-cli.json file works for me:

"styles": [     ...     "../node_modules/font-awesome/scss/font-awesome.scss",   ], 

Update 2

Changed prefixes to 'Mat' to reflect recent updates to angular material.

like image 67
SeanStanden Avatar answered Oct 07 '22 00:10

SeanStanden