Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use <md-icon> in Angular Material?

I was wondering how to use Material's icons, as this is not working:

<material-icon icon = "/img/icons/ic_access_time_24px.svg"> </material-icon>  

I guess there is a problem with the path given as parameter to the the icon attribute. I would like to know where this icon folder actually is?

like image 827
Adil Malik Avatar asked Oct 04 '14 11:10

Adil Malik


People also ask

What is MD in Angular material?

The "md" stands for "Material Design", a specific UI look-and-feel developed by Google in 2014. These are not part of Angular itself, but part of a component library built in Angular: "a set of reusable, well-tested, and accessible UI components based on Material Design": https://material.angularjs.org/latest/

How do I add a material icon?

To embed Google Icons in your HTML web pages you have to add the link of material-icons font library in the <head> section of your HTML file and then add the class material-icons in the <i> or <span> tag of the <body> section along with the name of the icon.


2 Answers

As the other answers didn't address my concern I decided to write my own answer.

The path given in the icon attribute of the md-icon directive is the URL of a .png or .svg file lying somewhere in your static file directory. So you have to put the right path of that file in the icon attribute. p.s put the file in the right directory so that your server could serve it.

Remember md-icon is not like bootstrap icons. Currently they are merely a directive that shows a .svg file.

Update

Angular material design has changed a lot since this question was posted.

Now there are several ways to use md-icon

The first way is to use SVG icons.

<md-icon md-svg-src = '<url_of_an_image_file>'></md-icon>

Example:

<md-icon md-svg-src = '/static/img/android.svg'></md-icon>

or

<md-icon md-svg-src = '{{ getMyIcon() }}'></md-icon>

:where getMyIcon is a method defined in $scope.

or <md-icon md-svg-icon="social:android"></md-icon>

to use this you have to the $mdIconProvider service to configure your application with svg iconsets.

angular.module('appSvgIconSets', ['ngMaterial'])     .controller('DemoCtrl', function($scope) {})     .config(function($mdIconProvider) {     $mdIconProvider       .iconSet('social', 'img/icons/sets/social-icons.svg', 24)       .defaultIconSet('img/icons/sets/core-icons.svg', 24);       }); 

The second way is to use font icons.

<md-icon md-font-icon="android" alt="android"></md-icon>

<md-icon md-font-icon="fa-magic" class="fa" alt="magic wand"></md-icon>

prior to doing this you have to load the font library like this..

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

or use font icons with ligatures

<md-icon md-font-library="material-icons">face</md-icon>

<md-icon md-font-library="material-icons">#xE87C;</md-icon>

<md-icon md-font-library="material-icons" class="md-light md-48">face</md-icon>

For further details check our

Angular Material mdIcon Directive documentation

$mdIcon Service Documentation

$mdIconProvider Service Documentation

like image 112
Adil Malik Avatar answered Sep 21 '22 17:09

Adil Malik


The simplest way today would be to simply request the Material Icons font from Google Fonts, for example in your HTML header tag:

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

or in your stylesheet:

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

and then use as font icon with ligatures as explained in the md-icon directive. For example:

<md-icon aria-label="Menu" class="material-icons">menu</md-icon> 

The complete list of icons/ligatures is at https://www.google.com/design/icons/

like image 32
Jesús Carrera Avatar answered Sep 25 '22 17:09

Jesús Carrera