0

I need to create an SVG tag in my component. Inside that SVG, I want to create unknown number of polygon components, like polygons, circles, rectangles, and so on. So, I have the data stored in array like:

polygons = [
    {
        type: 'polygon',
        points: "30,0 0,60 60,60"
    },{
        type: 'circle',
        x: 30,
        y: 30,
        r: 30
    }
    ...
]

This array is being created randomly, and it could have thousands of polygons. So how can we do this in angular 2? In react it was straight forward, but it seems in angular it is kind of tricky.

And one more question, are SVG tags are treated differently in Angular? or they are treated like all other HTML tags?

Any help will be appreciated...

1 Answer 1

3

There are different ways to do that (ngIf, ngSwitch, ngTemplateOutlet, ngComponentOutlet, ...), but i would use the following trick

<svg width="120" height="120" viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg">
  <ng-container *ngFor="let item of polygons">
      {
        item.type,
        yurzui,
        polygon { <svg:polygon [attr.points]="item.points"/> }
        circle  { <svg:circle [attr.cx]="item.x" [attr.cy]="item.y" [attr.r]="item.r"/>/> }
      }
  </ng-container>
</svg>

Plunker Example

Are SVG tags are treated differently in Angular? or they are treated like all other HTML tags?

I think they are treated like all other HTML tags, but they should be inside svg tag directly otherwise we have to use namespace like in my example <svg:circle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.