In angular, I am displaying an SVG:
<div [innerHTML]="svgImage" class="center">
</div>
The SVG contains polygons with a unique ID, for example:
<polyline id="tp_31" fill="#FFFAEC" points="698.814,494.607 719.188,459.31 698.813,424.014 658.064,424.015 637.69,459.313
658.065,494.608 698.814,494.607 "/>
I am able to style each polygon individually in the SCSS file with an id selector:
#tp_31:hover {
fill: blue;
}
However, I am not sure how to do this dynamically. The SVG image will be coming from a server and I would like to style it programatically. How can I best achieve this? I’ve tried using [ngStyle]
but I am not sure how to use it with an id selector. Thanks!
2
Answers
To dynamically style elements in an SVG that’s being injected with
[innerHTML]="svgImage"
, you can utilize Angular’s Renderer2 to manipulate the SVG content after it has been rendered.Create a ViewChild to reference the container:
Inject the SVG content with [innerHTML]:
Use Renderer2 to set styles:
you can interact with DOM directly
your second option is using render