moshi
moshi

Reputation: 294

font awesome with Angular 2

I started an NG2 app and wanna add font awesome. I npm installed it with : npm install --save font-awesome angular2-font-awesome. I added to project in systemjs.config.js:

map: {
  // our app is within the app folder
  app: 'app',

  // angular bundles
  '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
  '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
  '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
  '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
  '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
  '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
  '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
  '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

  // other libraries
  'rxjs':                      'npm:rxjs',
  'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
  'angular2-fontawesome': 'node_modules/angular2-fontawesome'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
  app: {
    defaultExtension: 'js'
  },
  rxjs: {
    defaultExtension: 'js'
  },
  'angular2-fontawesome': { defaultExtension: 'js' }
}

and I added in app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import { CorporateComponent }  from './corporate/corporate.component';
import { Angular2FontawesomeModule } from 'angular2-fontawesome/angular2-fontawesome'

@NgModule({
  imports:      [ BrowserModule , Angular2FontawesomeModule],
  declarations: [ AppComponent, CorporateComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Now I try to use it in my component: I add in the html template:

<i class="fa fa-industry" aria-hidden="true"></i>

But somehow I don't see it... I followed the manual of npm: https://www.npmjs.com/package/angular2-fontawesome

Is there anything else that I might have forgotten ?

Thanks for any answer!

Upvotes: 6

Views: 6862

Answers (5)

Ernesto Silva
Ernesto Silva

Reputation: 1

If you're using SCSS, you can go with the following method:

npm install --save font-awesome

Then in the file style.scss

$fa-version: '4.7';
$fa-font-path: '../fonts';
@import '../node_modules/font-awesome/scss/font-awesome.scss';

$fa-font-path must be relative to font-awesome.scss.

$fa-version does not need to be defined, but if you check the file node_modules/font-awesome/scss/_path.scss, you'll notice the url's like:

...
src: url('#{$fa-font-path}/fontawesome-webfont.eot?v=#{$fa-version}');
...

Upvotes: 0

Nitin Manocha
Nitin Manocha

Reputation: 140

Try to use this method:

Firstly install font awesome using command : npm install --save font-awesome angular-font-awesome

secondly, import it in your style.scss file with

$fa-font-path: "../node_modules/font-awesome/fonts";
@import "../node_modules/font-awesome/scss/font-awesome.scss";

or to your style.css file with

@import '~font-awesome/css/font-awesome.css';

command. The fonts are ready to use.

Now place your font in any html file. <i class="fa facebook"></i>

Upvotes: 0

Jay
Jay

Reputation: 43

I think you need to add it to your .angular-cli.json section like this:

you may have it under a different name in your node_modules. try to go down the list in node_modules and see under what name it has been installed. The other thing I noticed is npm install --save font-awesome angular-font-awesome

`"styles": [
    "styles.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css",
    "../node_modules/font-awesome/css/font-awesome.css",
    "../node_modules/npm-font-open-sans/open-sans.styl"


  ],

Upvotes: 0

Aravind
Aravind

Reputation: 41533

Not much of effort is needed just include the script tag.

<div>
      <h2>Hello {{name}}</h2>
      <i class="fa fa-home"></i>
</div>

and my index.html contains script reference to the above script URL.

enter image description here

Get your script code by Registering Here. You will receive a mail from font-awesome as below enter image description here

LIVE DEMO

Upvotes: 0

Tiep Phan
Tiep Phan

Reputation: 12596

if you wanna using FontAwesome css style in your app, just put css link to your index.html, you don't need angular2-font-awesome

index.html

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

if you using angular2-font-awesome

<i fa [name]="industry"></i>
<fa [name]="industry"></fa>

i think use cdn is better.

Upvotes: 6

Related Questions