22

I'm trying to use angular material 2 to display icons on my website, but I'm a little confused.

This is how it's supposed to work, from the demo in github repo of material 2:

https://github.com/angular/material2/blob/master/src/demo-app/icon/icon-demo.ts

I've been trying to use it but no icons are shown at all.

This is how I set it up:

app.component.ts

import {MdIcon, MdIconRegistry} from '@angular2-material/icon/icon';

@Component({
  ...
  encapsulation: ViewEncapsulation.None,
  viewProviders: [MdIconRegistry],
  directives: [MdIcon],
})
export class MyComponent{
  constructor(private router: Router,
              private JwtService:JwtService,
              mdIconRegistry: MdIconRegistry){
    mdIconRegistry.addSvgIconSetInNamespace('core', 'fonts/core-icon-set.svg')
  }
}

and the template..

<md-icon>home</md-icon>

The page loads with no errors, but no icon is shown. What could have gone wrong?

5
  • Did you investigate the resulting DOM. How does it look like? Commented May 12, 2016 at 17:35
  • I'm sorry @GünterZöchbauer, I don't understand what do you mean by that. Commented May 12, 2016 at 17:36
  • 1
    Open browser devtools and check how the result DOM looks like. I guess the icon-demo is somewhere available online as well. You can compare the DOM of the icon in your application and the one in the demo. Maybe it gives some hint what might have went wrong. Commented May 12, 2016 at 17:38
  • This should be closed. The answers are extremely out of date and misleading at this point. Commented Oct 11, 2017 at 19:20
  • @BenRacicot Even if the question is closed because of the number of votes on both the question and answer it would still hang around. If the answers are wrong then downvote and/or comment on the wrong answers and/or post your own answer that is correct. There is no reason to vote to close a question because the answers or wrong Commented Oct 11, 2017 at 19:54

9 Answers 9

32

In order to use MdIcon, you need to include the corresponding css files. In your code, you are using the default font which is Material Icons from google.

From angular-material2 repo:

By default the Material icons font is used. (You will still need to include the HTML to load the font and its CSS, as described in the link).

Simply, just include the css in index.html like this:

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Or you can choose any other method of importing mentioned in the official repo:

http://google.github.io/material-design-icons/#icon-font-for-the-web

Sign up to request clarification or add additional context in comments.

4 Comments

Does this still apply with the move to a singular @angular/material?
@redfox05 yes it does, I updated the documentation link in the answer.
If you are using some type of autoreload/compiling, like angular-cli (ng serve), remember cancel de service and relaunch ng serve
This works with the Material Icons font, but doesn't seem to work with the SVG icon set as the questioner was trying to do. Did anyone achieve this functionality?
16

It's worth to know that to use an icon space separated (for example file upload) we need to use underscore _ . For example:

<md-icon>file_upload</md-icon>

2 Comments

Good advice, but this doesn't answer the question.
Definitely helpful thanks Milso, nowhere does it say to replace spaces with underscores.
14

As of @ngModule introduction starting from Angular RC5 syntax would be as follow:

app-example-module.ts

import { MdIconModule, MdIconRegistry } from '@angular2-material/icon';

@NgModule({
    imports: [
        MdIconModule
    ]
    providers: [
        MdIconRegistry
    ]
})

export class AppExampleModule {
    //
}

app-example-component.ts

@Component({

    selector: 'app-header',
    template: `<md-icon svgIcon="close"></md-icon>`
})


export class AppExampleComponent
{
    constructor(private mdIconRegistry: MdIconRegistry) {
        mdIconRegistry
            .addSvgIcon('close', '/icons/navigation/ic_close_36px.svg');
    }
}

1 Comment

my man, this is what I was looking for!!! as a heads up to others, you can import from @angular/material now
11

inside style.css copy and paste the following:---

@import "https://fonts.googleapis.com/icon?family=Material+Icons";

and use like:

<md-icon>menu</md-icon>
          ^--- icon name

Comments

5

In angular 4.3.3 with @angular/material 2.0.0-beta-7 you also need to sanitize the url.

import { DomSanitizer } from '@angular/platform-browser';

export class AppComponent
{
    constructor(
        private domSanitizer: DomSanitizer,
        private mdIconRegistry: MdIconRegistry) {
        mdIconRegistry.addSvgIcon('twitter', domSanitizer.bypassSecurityTrustResourceUrl('/assets/icons/twitter.svg'));
    }
}

Comments

3

To work offline/local(provide css from your server):

  1. npm install material-design-icons --save
  2. in src/styles.css add @import "~material-design-icons/iconfont/material-icons.css";

Comments

0

This is the way, I tried and it works.

mdIconRegistry.addSvgIcon('slider', sanitizer.bypassSecurityTrustResourceUrl('./assets/controls/slider.svg'));

The instance mdIconRegistry will be available via DI and add custom svg using addSvgIcon method. Then use <md-icon svgIcon="slider"> in your template.

Comments

0

Just add the following line on index.html

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Comments

-3

Also you need to import Http.

import {HTTP_PROVIDERS} from '@angular/http';
import {MdIcon, MdIconRegistry} from  '@angular2-material/icon';
@Component({
    template:`<md-icon>code</md-icon>`
    directives:[MdIcon],
    providers: [HTTP_PROVIDERS, MdIconRegistry]
})

1 Comment

Would you please explain further? With the new NgModule API, this doesn't seem right to me...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.