The angular-svg-icon is an Angular 2+ service and component that provides a means to inline SVG images to allow for them to be easily styled by CSS and code.
The service provides an icon registery that loads and caches a svg indexed by
its url. The component is responsible for displaying the svg. After getting the
svg from the registry it clones the SVGElement
and the svg to the component's
inner HTML.
A working demo shows solution in action.
$ npm i angular-svg-icon --save
The angular-svg-icon should work as-is with webpack/angular-cli. Just add
the AngularSvgIconModule
.
import { AngularSvgIconModule } from 'angular-svg-icon';
@NgModule({
imports: [ AngularSvgIconModule ],
...
})
export class AppModule {}
Basic usage is:
<svg-icon src="path/to/icon.svg"></svg-icon>
An example of styling the svg:
<svg-icon src="images/eye.svg" style="fill:green;width:90px;"></svg-icon>
The svg-icon is an Angular 2 component that allows for the continuation of the AngularJS method for easily inlining SVGs explained by Ben Markowitz and others. Including the SVG source inline allows for the graphic to be easily styled by CSS.
The technique made use of ng-include to inline the svg source into the document. Angular 2, however, drops the support of ng-include, so this is my work-around method.
Note: The icon component from angular/material2 used to have a direct means to load svg similar to this, but this functionality was removed because of security concerns.
MIT
- David Czeck @czeckd