My goal is to create a custom component that can dynamically load and display the content of an SVG file in its template.
So far, I have attempted the following approach:
icon.component.ts
...
@Component({
selector: 'app-icon',
template: `
<div [innerHTML]="svgTemplate">
</div>
`,
})
export class IconComponent implements OnInit {
@Input() name: string;
private svgTemplate: string;
constructor(
private http: Http
) { }
ngOnInit() {
this.http.get(`assets/icon/ic_${this.name}.svg`).map(response => response.text()).subscribe(svg => {
this.svgTemplate = svg;
});
}
}
However, the SVG content appears to be escaped, as nothing is showing up in the template div
.
Below is an example of SVG content:
<svg version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 52.4 44.1" style="enable-background:new 0 0 52.4 44.1;" xml:space="preserve">
<style type="text/css">
.st0{fill:#444444;}
</style>
<g>
<g transform="translate(312, 312)">
<path class="st0" d="M-259.6-312H-312v6.4h52.4V-312z"/>
</g>
<g transform="translate(312, 312)">
<path class="st0" d="M-259.6-293.1H-312v6.4h52.4V-293.1z"/>
</g>
<g transform="translate(312, 312)">
<path class="st0" d="M-259.6-274.3H-312v6.4h52.4V-274.3z"/>
</g>
</g>
</svg>