Description
Reproduction
svgIcon
does not seem to work with Stackblitz, providing repo instead.
Steps to reproduce:
- Clone https://github.com/klemenoslaj/mat-icon-svg-not-applied
- Install dependencies using
yarn
- Execute
ng serve
Expected Behavior
I tried to create a custom directive that would programmatically assign some icon name to the svgIcon
input of mat-icon
, depending on some global icon mapping configuration.
This normally works for any component and I'd expect it should for mat-icon
too.
Example:
<mat-icon [iconFor]="object"></mat-icon>
@Directive({
selector: 'mat-icon[iconFor]',
})
export class IconForDirective {
@Input()
set iconFor(obj: SomeInterface) {
this._matIcon.svgIcon = _iconMap[obj.type];
}
constructor(
@Inject(ICON_MAPPING_TOKEN)
private readonly _iconMap: SomeIconMapping,
private readonly _matIcon: MatIcon,
) {}
}
Actual Behavior
The behavior described above does not work with mat-icon
, because the relevant logic is in ngOnChanges
hook of mat-icon
, which does not get triggered, unless at least one Input
is present on the host component.
components/src/material/icon/icon.ts
Lines 229 to 264 in 4f1238f
Workaround (ugly):
Instead of
<mat-icon [iconFor]="object"></mat-icon>
add dummy/empty svgIcon
<mat-icon [iconFor]="object" svgIcon></mat-icon>
Environment
- Angular: 10.0.14
- CDK/Material: 10.1.3
- Browser(s): any
- Operating System (e.g. Windows, macOS, Ubuntu): any
NOTE: I can submit the PR for this, but would like some thoughts on that, maybe it's expected like this for some reason.