@@ -70,6 +70,8 @@ export class MdIconInvalidNameError extends MdError {
7070 changeDetection : ChangeDetectionStrategy . OnPush ,
7171} )
7272export class MdIcon implements OnChanges , OnInit , AfterViewChecked {
73+ private _color : string ;
74+
7375 @Input ( ) svgSrc : string ;
7476 @Input ( ) svgIcon : string ;
7577 @Input ( ) fontSet : string ;
@@ -78,14 +80,35 @@ export class MdIcon implements OnChanges, OnInit, AfterViewChecked {
7880
7981 @Input ( 'aria-label' ) hostAriaLabel : string = '' ;
8082
83+ @Input ( )
84+ get color ( ) : string {
85+ return this . _color ;
86+ }
87+
88+ set color ( value : string ) {
89+ this . _updateColor ( value ) ;
90+ }
91+
8192 private _previousFontSetClass : string ;
8293 private _previousFontIconClass : string ;
8394
8495 constructor (
85- private _element : ElementRef ,
96+ private _elementRef : ElementRef ,
8697 private _renderer : Renderer ,
8798 private _mdIconRegistry : MdIconRegistry ) { }
8899
100+ _updateColor ( newColor : string ) {
101+ this . _setElementColor ( this . _color , false ) ;
102+ this . _setElementColor ( newColor , true ) ;
103+ this . _color = newColor ;
104+ }
105+
106+ _setElementColor ( color : string , isAdd : boolean ) {
107+ if ( color != null && color != '' ) {
108+ this . _renderer . setElementClass ( this . _elementRef . nativeElement , `md-${ color } ` , isAdd ) ;
109+ }
110+ }
111+
89112 /**
90113 * Splits an svgIcon binding value into its icon set and icon name components.
91114 * Returns a 2-element array of [(icon set), (icon name)].
@@ -156,7 +179,7 @@ export class MdIcon implements OnChanges, OnInit, AfterViewChecked {
156179 private _updateAriaLabel ( ) {
157180 const ariaLabel = this . _getAriaLabel ( ) ;
158181 if ( ariaLabel ) {
159- this . _renderer . setElementAttribute ( this . _element . nativeElement , 'aria-label' , ariaLabel ) ;
182+ this . _renderer . setElementAttribute ( this . _elementRef . nativeElement , 'aria-label' , ariaLabel ) ;
160183 }
161184 }
162185
@@ -174,7 +197,7 @@ export class MdIcon implements OnChanges, OnInit, AfterViewChecked {
174197 }
175198 // The "content" of an SVG icon is not a useful label.
176199 if ( this . _usingFontIcon ( ) ) {
177- const text = this . _element . nativeElement . textContent ;
200+ const text = this . _elementRef . nativeElement . textContent ;
178201 if ( text ) {
179202 return text ;
180203 }
@@ -188,7 +211,7 @@ export class MdIcon implements OnChanges, OnInit, AfterViewChecked {
188211 }
189212
190213 private _setSvgElement ( svg : SVGElement ) {
191- const layoutElement = this . _element . nativeElement ;
214+ const layoutElement = this . _elementRef . nativeElement ;
192215 // Remove existing child nodes and add the new SVG element.
193216 // We would use renderer.detachView(Array.from(layoutElement.childNodes)) here,
194217 // but it fails in IE11: https://github.com/angular/angular/issues/6327
@@ -200,7 +223,7 @@ export class MdIcon implements OnChanges, OnInit, AfterViewChecked {
200223 if ( ! this . _usingFontIcon ( ) ) {
201224 return ;
202225 }
203- const elem = this . _element . nativeElement ;
226+ const elem = this . _elementRef . nativeElement ;
204227 const fontSetClass = this . fontSet ?
205228 this . _mdIconRegistry . classNameForFontAlias ( this . fontSet ) :
206229 this . _mdIconRegistry . getDefaultFontSetClass ( ) ;
0 commit comments