UPDATE Feb 2020:
fortawesome package now supports ng add
but it is available only for angular 9 and up:
ng add @fortawesome/angular-fontawesome
UPDATE 8 Oct 2019:
You can use a new package https://www.npmjs.com/package/@fortawesome/angular-fontawesome
npm install @fortawesome/angular-fontawesome @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons
Add FontAwesomeModule
to imports in src/app/app.module.ts
:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'; @NgModule({ imports: [ BrowserModule, FontAwesomeModule ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { }
Tie the icon to the property in your component src/app/app.component.ts
:
import { Component } from '@angular/core'; import { faCoffee } from '@fortawesome/free-solid-svg-icons'; @Component({ selector: 'app-root', templateUrl: './app.component.html' }) export class AppComponent { faCoffee = faCoffee; }
Use the icon in the template src/app/app.component.html
:
<fa-icon [icon]="faCoffee"></fa-icon>
ORIGINAL ANSWER:
Option 1:
You can use angular-font-awesome npm module
npm install --save font-awesome angular-font-awesome
Import the module:
... // import { AngularFontAwesomeModule } from 'angular-font-awesome'; @NgModule({ //... imports: [ //... AngularFontAwesomeModule ], //... }) export class AppModule { }
If you're using Angular CLI, add the font-awesome CSS to styles inside the angular-cli.json
"styles": [ "styles.css", "../node_modules/font-awesome/css/font-awesome.css" ],
NOTE: If using SCSS preprocessor just change the css for scss
Example Use:
<fa name="cog" animation="spin"></fa>
Option 2:
There is an official story for that now
Install the font-awesome library and add the dependency to package.json
npm install --save font-awesome
Using CSS
To add Font Awesome CSS icons to your app...
// in .angular-cli.json "styles": [ "styles.css", "../node_modules/font-awesome/css/font-awesome.css" ]
Using SASS
Create an empty file _variables.scss
in src/
.
Add the following to _variables.scss
:
$fa-font-path : '../node_modules/font-awesome/fonts';
In styles.scss
add the following:
@import 'variables'; @import '../node_modules/font-awesome/scss/font-awesome';
Test
Run ng serve to run your application in develop mode, and navigate to http://localhost:4200.
To verify Font Awesome has been set up correctly, change src/app/app.component.html
to the following...
<h1> {{title}} <i class="fa fa-check"></i> </h1>
After saving this file, return to the browser to see the Font Awesome icon next to the app title.
Also there is a related question Angular CLI outputs the font-awesome font files the dist root as by default angular cli outputs the fonts in to the dist
root, which is by the way not an issue at all.