Colton Williams Colton Williams - 18 days ago 6
TypeScript Question

Using material icons with md-icon in Angular Material 2

I cannot get md-icon to display the icons from Google Material. For those who don't need much exposition here is the code, more details are after the code.

app.component(where I display all of my main components, the actual md-icon is in header.component):

import { Component } from '@angular/core';
import { HeaderComponent } from './shared/header.component';
import { ContentComponent } from './shared/content.component';
import { FooterComponent } from './shared/footer.component';

@Component({
selector: 'my-app',
template: `<div id="wrapper">
<header></header>
<content></content>
<footer></footer>
<div>`,
directives: [HeaderComponent, ContentComponent, FooterComponent]
})
export class AppComponent { }


Header component (where I try to call md-icon):

import { Component } from '@angular/core';
import {MdIcon} from '@angular2-material/icon';

@Component({
selector: 'header',
template: `<md-icon>home</md-icon>
<h1>This is the Header</h1>
`,
directives: [MdIcon]
})
export class HeaderComponent { }


and here is the css and html according to the steps on google materials site for using their icons:

CSS:

@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url("../fonts/MaterialIcons-Regular.eot");
/* For IE6-8 */
src: local("Material Icons"), local("MaterialIcons-Regular"), url("../fonts/MaterialIcons-Regular.woff2") format("woff2"), url("../fonts/MaterialIcons-Regular.woff") format("woff"), url("../fonts/MaterialIcons-Regular.ttf") format("truetype");
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
/* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
/* Support for all WebKit browsers. */
-webkit-font-smoothing: antialiased;
/* Support for Safari and Chrome. */
text-rendering: optimizeLegibility;
/* Support for Firefox. */
-moz-osx-font-smoothing: grayscale;
/* Support for IE. */
font-feature-settings: 'liga';
}


HTML:

<html>

<head>
<title>Boiler Plate</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/styles/layout.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err) {
console.error(err);
});
</script>
</head>
<!-- 3. Display the application -->

<body>
<my-app>Loading...</my-app>
</body>

</html>


Originally I was not getting any errors it was simply rendering the md-icon as "home" in plain text. However I think I was missing the directive for MdIcon, so I inserted that and now I recieve this error "No provider for MdIconRegistry!". They talk a little bit about the MdIconRegistry in their docs but from the way I am reading it you only need that if you don't want to use the standard google icons, which I do want to use. So basically I imported the component, included the directive, added the link in my index, and added the css code to my stylesheets. From what I have read it should work but I am clearly missing something.

Any help would be appreciated and if anyone has any critiques on the code unrelated to the actual question please feel free to let me know, I am very new to Angular 2 and I could use all the advice I can get.

Thanks in advance.

Answer

Ok so I found what I was missing, it was in the docs, I just wasn't looking in the right place. Inside of your main.ts file you need to import and bootstrap HTTP_Providers and inside of your component you need to import MdIcon and MdIconRegistry as well as include the provider for MdIconRegistry. What does all of that mean? I am glad you asked!

Main.ts before:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';

bootstrap(AppComponent);

Main.ts after:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { HTTP_PROVIDERS } from '@angular/http';

bootstrap(AppComponent, [
    HTTP_PROVIDERS
]);

header.component.ts before:

import { Component } from '@angular/core';
import {MdIcon} from '@angular2-material/icon';

@Component({
    selector: 'header',
    template: `<md-icon>home</md-icon>`,
    directives: [MdIcon]
})
export class HeaderComponent { }

header.component.ts after:

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

@Component({
    selector: 'header',
    template: `<md-icon>home</md-icon>`,
    directives: [MdIcon],
    providers: [MdIconRegistry]
})
export class HeaderComponent { }

The documentation for the icons can be found here and the documentation that helped me can be found here at the very bottom.

Comments