Skip to content

Instantly share code, notes, and snippets.

@thrixton
Created November 16, 2018 04:47
Show Gist options
  • Save thrixton/8ddf2eb709d36b7a80120ad2834e5be4 to your computer and use it in GitHub Desktop.
Save thrixton/8ddf2eb709d36b7a80120ad2834e5be4 to your computer and use it in GitHub Desktop.
icon.service.ts
import { Injectable } from '@angular/core';
import { MatIconRegistry } from '@angular/material';
import { DomSanitizer } from '@angular/platform-browser';
import { IconDefinition } from '@fortawesome/fontawesome-svg-core';
@Injectable({
providedIn: 'root'
})
export class IconService {
constructor(private _iconRegistry: MatIconRegistry, private _sanitizer: DomSanitizer) {}
addSvg(icon: IconDefinition) {
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${icon.icon[0]} ${icon.icon[1]}">
<path d="${icon.icon[4]}" />
</svg>`;
this._iconRegistry.getNamedSvgIcon(icon.iconName, icon.prefix).subscribe(
() => {
//Ok, icon already exists
},
() => {
//Error, not found, add it
this._iconRegistry.addSvgIconLiteralInNamespace(
icon.prefix,
icon.iconName,
this._sanitizer.bypassSecurityTrustHtml(svg)
);
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment