Angular svg matching component with svg elements
NickName:Yona Appletree Ask DateTime:2018-01-26T07:23:05

Angular svg matching component with svg elements

Consider the following component that matches <svg icon="close"></svg> for the purpose of displaying an icon:

@Component({
    selector: 'svg[icon]',
    template: `<use [attr.xlink:href]="'icons.svg#'+icon"></use>`
})
export class SvgIconComponent {
    @Input() icon: string;
}

This fails because Angular can't find the <use> element, as it is unaware that the content of the component is inside an <svg>. It would be nice if Angular would notice that the selector ensures this truth, but it does not.

The exact error is:

Uncaught Error: Template parse errors:
'use' is not a known element:
    1. If 'use' is an Angular component, then verify that it is part of this module.
    2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. 

Obviously I could put the NO_ERRORS_SCHEMA on the module of the Component, but I'd prefer not to, since there are other components in the module, and because I want the unknown element error checking to work.

So, how can I tell Angular that it should treat the content of this particular component as being SVG content? If that's not possible, is there some other way to do this cleanly?

Copyright Notice:Content Author:「Yona Appletree」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48453514/angular-svg-matching-component-with-svg-elements

More about “Angular svg matching component with svg elements” related questions

Angular svg matching component with svg elements

Consider the following component that matches &lt;svg icon="close"&gt;&lt;/svg&gt; for the purpose of displaying an icon: @Component({ selector: 'svg[icon]', template: `&lt;use [attr.xlink..

Show Detail

Measuring dimensions of svg elements in Angular

I am trying to create a gauge like component in Angular using SVG to draw the shapes. I would like to center the text within a rectangle. The text will change depending on the value of the gauge,

Show Detail

Angular - How to insert dynamic SVG into DOM via component

I am trying to insert SVG elements into the DOM using an angular component, and having problems. My app is to display a toolbox of Icons that I can drag onto a workspace. the Icons are SVG based....

Show Detail

Angular nested SVG component is invisible

I'm using two SVG components as described here. Now I want to use one component inside the other. But when I do this the nested component is invisible. There is no error and with the debugger tool,...

Show Detail

Angular 12 doesn't render svg component

I am developing an angular component. it is an avatar creater using with svg. My problem is that when I run my component in angular project it draws just a circle not other components like nose,hai...

Show Detail

Angular svg as template for component shown as text

I'm currently trying to display a svg icon by using Angular component like this import {Component} from '@angular/core'; @Component({ selector: 'app-profile-icon', templateUrl: './profile...

Show Detail

Angular 5, import SVG file into component

I have def.svg file, I want to import this file into my angular 5 component. Is it possible to import it using angular-cli? If yes, How can I import it? Or Is any other way exists in angular 5? de...

Show Detail

Angular: component should produce parts of a SVG

An Angular component normally produces HTML. If I try to let them produce SVG it failes because it does not know the SVG tags. Seems it only knows them if the result is inside . But thats not the c...

Show Detail

Use relative path for svg in Angular Library Component

I created a custom Library with Angular Cli. It has the following structure: - dist - projects - customLib - src - assets - icons - lib ...

Show Detail

How to load SVG synchronous by using snap.svg in Angular 2 component constructor?

Please point me. I know snap.load() is a async call but I want to make it synchronously inside of component constructor so that when construction finish I have have all the svg elements ready for u...

Show Detail