Created
November 16, 2018 04:47
-
-
Save thrixton/8ddf2eb709d36b7a80120ad2834e5be4 to your computer and use it in GitHub Desktop.
icon.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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