@@ -14,7 +14,7 @@ import {
14
14
OnChanges ,
15
15
OnInit ,
16
16
Renderer2 ,
17
- SimpleChange ,
17
+ SimpleChanges ,
18
18
ViewEncapsulation ,
19
19
Attribute ,
20
20
} from '@angular/core' ;
@@ -33,12 +33,6 @@ export const _MdIconMixinBase = mixinColor(MdIconBase);
33
33
34
34
/**
35
35
* Component to display an icon. It can be used in the following ways:
36
- * - Specify the svgSrc input to load an SVG icon from a URL. The SVG content is directly inlined
37
- * as a child of the <md-icon> component, so that CSS styles can easily be applied to it.
38
- * The URL is loaded via an XMLHttpRequest, so it must be on the same domain as the page or its
39
- * server must be configured to allow cross-domain requests.
40
- * Example:
41
- * <md-icon svgSrc="assets/arrow.svg"></md-icon>
42
36
*
43
37
* - Specify the svgIcon input to load an SVG icon from a URL previously registered with the
44
38
* addSvgIcon, addSvgIconInNamespace, addSvgIconSet, or addSvgIconSetInNamespace methods of
@@ -134,17 +128,16 @@ export class MdIcon extends _MdIconMixinBase implements OnChanges, OnInit, CanCo
134
128
}
135
129
}
136
130
137
- ngOnChanges ( changes : { [ propertyName : string ] : SimpleChange } ) {
138
- const changedInputs = Object . keys ( changes ) ;
131
+ ngOnChanges ( changes : SimpleChanges ) {
139
132
// Only update the inline SVG icon if the inputs changed, to avoid unnecessary DOM operations.
140
- if ( changedInputs . indexOf ( 'svgIcon' ) != - 1 || changedInputs . indexOf ( 'svgSrc' ) != - 1 ) {
141
- if ( this . svgIcon ) {
142
- const [ namespace , iconName ] = this . _splitIconName ( this . svgIcon ) ;
143
- first . call ( this . _mdIconRegistry . getNamedSvgIcon ( iconName , namespace ) ) . subscribe (
144
- svg => this . _setSvgElement ( svg ) ,
145
- ( err : Error ) => console . log ( `Error retrieving icon: ${ err . message } ` ) ) ;
146
- }
133
+ if ( changes . svgIcon && this . svgIcon ) {
134
+ const [ namespace , iconName ] = this . _splitIconName ( this . svgIcon ) ;
135
+
136
+ first . call ( this . _mdIconRegistry . getNamedSvgIcon ( iconName , namespace ) ) . subscribe (
137
+ svg => this . _setSvgElement ( svg ) ,
138
+ ( err : Error ) => console . log ( `Error retrieving icon: ${ err . message } ` ) ) ;
147
139
}
140
+
148
141
if ( this . _usingFontIcon ( ) ) {
149
142
this . _updateFontIconClasses ( ) ;
150
143
}
@@ -164,21 +157,27 @@ export class MdIcon extends _MdIconMixinBase implements OnChanges, OnInit, CanCo
164
157
165
158
private _setSvgElement ( svg : SVGElement ) {
166
159
const layoutElement = this . _elementRef . nativeElement ;
167
- // Remove existing child nodes and add the new SVG element.
168
- // We would use renderer.detachView(Array.from(layoutElement.childNodes)) here,
169
- // but it fails in IE11: https://github.com/angular/angular/issues/6327
170
- layoutElement . innerHTML = '' ;
160
+ const childCount = layoutElement . childNodes . length ;
161
+
162
+ // Remove existing child nodes and add the new SVG element. Note that we can't
163
+ // use innerHTML, because IE will throw if the element has a data binding.
164
+ for ( let i = 0 ; i < childCount ; i ++ ) {
165
+ this . _renderer . removeChild ( layoutElement , layoutElement . childNodes [ i ] ) ;
166
+ }
167
+
171
168
this . _renderer . appendChild ( layoutElement , svg ) ;
172
169
}
173
170
174
171
private _updateFontIconClasses ( ) {
175
172
if ( ! this . _usingFontIcon ( ) ) {
176
173
return ;
177
174
}
175
+
178
176
const elem = this . _elementRef . nativeElement ;
179
177
const fontSetClass = this . fontSet ?
180
178
this . _mdIconRegistry . classNameForFontAlias ( this . fontSet ) :
181
179
this . _mdIconRegistry . getDefaultFontSetClass ( ) ;
180
+
182
181
if ( fontSetClass != this . _previousFontSetClass ) {
183
182
if ( this . _previousFontSetClass ) {
184
183
this . _renderer . removeClass ( elem , this . _previousFontSetClass ) ;
0 commit comments