Dynamically updating SVG in Angular










0














I am trying to programatically add paths to an SVG element in an HTML in an Angular application. The problem is that the paths are being added to the DOM but are not rendering in the browser. Despite a day of searching and experimenting, I can't find what the issue is. I've reproduced the problem in a small app. Hopefully someone can spot what I'm doing wrong.



Angular template for the component:



<div style="text-align:center">
Click the button to add a path to the svg
<button (click)="onClickMe()">Click me!</button>
</div>
<div>
<svg #svg
xmlns="http://www.w3.org/2000/svg"
width="200mm"
height="200mm"
viewBox="0 0 200 200">
<path
style="fill:#000000;fill-opacity:1;stroke:#000000;"
d="M 60,147 h 40 v 30 H 85 V 157 H 75 v 20 H 60 Z"
id="path1">
</path>
</svg>
</div>


And the Typescript:



import Component, ElementRef, Renderer2, ViewChild from '@angular/core'

@Component(
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
)
export class AppComponent
@ViewChild('svg') svg: ElementRef

constructor(private renderer: Renderer2)


onClickMe()
const path = this.renderer.createElement("path", 'http://www.w3.org/2000/svg')
this.renderer.setAttribute(path, "d", 'M60,150 H50 V 50 Z')
this.renderer.setAttribute(path, "style", "fill:#F00;")

this.renderer.appendChild(this.svg.nativeElement, path)




When you run the app, the hardcoded path in the template is displayed correctly. However when you click on the button, a path is inserted as child of the SVG element but the path isn't rendering in the browser.










share|improve this question


























    0














    I am trying to programatically add paths to an SVG element in an HTML in an Angular application. The problem is that the paths are being added to the DOM but are not rendering in the browser. Despite a day of searching and experimenting, I can't find what the issue is. I've reproduced the problem in a small app. Hopefully someone can spot what I'm doing wrong.



    Angular template for the component:



    <div style="text-align:center">
    Click the button to add a path to the svg
    <button (click)="onClickMe()">Click me!</button>
    </div>
    <div>
    <svg #svg
    xmlns="http://www.w3.org/2000/svg"
    width="200mm"
    height="200mm"
    viewBox="0 0 200 200">
    <path
    style="fill:#000000;fill-opacity:1;stroke:#000000;"
    d="M 60,147 h 40 v 30 H 85 V 157 H 75 v 20 H 60 Z"
    id="path1">
    </path>
    </svg>
    </div>


    And the Typescript:



    import Component, ElementRef, Renderer2, ViewChild from '@angular/core'

    @Component(
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
    )
    export class AppComponent
    @ViewChild('svg') svg: ElementRef

    constructor(private renderer: Renderer2)


    onClickMe()
    const path = this.renderer.createElement("path", 'http://www.w3.org/2000/svg')
    this.renderer.setAttribute(path, "d", 'M60,150 H50 V 50 Z')
    this.renderer.setAttribute(path, "style", "fill:#F00;")

    this.renderer.appendChild(this.svg.nativeElement, path)




    When you run the app, the hardcoded path in the template is displayed correctly. However when you click on the button, a path is inserted as child of the SVG element but the path isn't rendering in the browser.










    share|improve this question
























      0












      0








      0







      I am trying to programatically add paths to an SVG element in an HTML in an Angular application. The problem is that the paths are being added to the DOM but are not rendering in the browser. Despite a day of searching and experimenting, I can't find what the issue is. I've reproduced the problem in a small app. Hopefully someone can spot what I'm doing wrong.



      Angular template for the component:



      <div style="text-align:center">
      Click the button to add a path to the svg
      <button (click)="onClickMe()">Click me!</button>
      </div>
      <div>
      <svg #svg
      xmlns="http://www.w3.org/2000/svg"
      width="200mm"
      height="200mm"
      viewBox="0 0 200 200">
      <path
      style="fill:#000000;fill-opacity:1;stroke:#000000;"
      d="M 60,147 h 40 v 30 H 85 V 157 H 75 v 20 H 60 Z"
      id="path1">
      </path>
      </svg>
      </div>


      And the Typescript:



      import Component, ElementRef, Renderer2, ViewChild from '@angular/core'

      @Component(
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
      )
      export class AppComponent
      @ViewChild('svg') svg: ElementRef

      constructor(private renderer: Renderer2)


      onClickMe()
      const path = this.renderer.createElement("path", 'http://www.w3.org/2000/svg')
      this.renderer.setAttribute(path, "d", 'M60,150 H50 V 50 Z')
      this.renderer.setAttribute(path, "style", "fill:#F00;")

      this.renderer.appendChild(this.svg.nativeElement, path)




      When you run the app, the hardcoded path in the template is displayed correctly. However when you click on the button, a path is inserted as child of the SVG element but the path isn't rendering in the browser.










      share|improve this question













      I am trying to programatically add paths to an SVG element in an HTML in an Angular application. The problem is that the paths are being added to the DOM but are not rendering in the browser. Despite a day of searching and experimenting, I can't find what the issue is. I've reproduced the problem in a small app. Hopefully someone can spot what I'm doing wrong.



      Angular template for the component:



      <div style="text-align:center">
      Click the button to add a path to the svg
      <button (click)="onClickMe()">Click me!</button>
      </div>
      <div>
      <svg #svg
      xmlns="http://www.w3.org/2000/svg"
      width="200mm"
      height="200mm"
      viewBox="0 0 200 200">
      <path
      style="fill:#000000;fill-opacity:1;stroke:#000000;"
      d="M 60,147 h 40 v 30 H 85 V 157 H 75 v 20 H 60 Z"
      id="path1">
      </path>
      </svg>
      </div>


      And the Typescript:



      import Component, ElementRef, Renderer2, ViewChild from '@angular/core'

      @Component(
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
      )
      export class AppComponent
      @ViewChild('svg') svg: ElementRef

      constructor(private renderer: Renderer2)


      onClickMe()
      const path = this.renderer.createElement("path", 'http://www.w3.org/2000/svg')
      this.renderer.setAttribute(path, "d", 'M60,150 H50 V 50 Z')
      this.renderer.setAttribute(path, "style", "fill:#F00;")

      this.renderer.appendChild(this.svg.nativeElement, path)




      When you run the app, the hardcoded path in the template is displayed correctly. However when you click on the button, a path is inserted as child of the SVG element but the path isn't rendering in the browser.







      javascript angular typescript dom svg






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 '18 at 19:17









      matt helliwell

      1,666916




      1,666916






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Angular keeps namespaces map in its code:



          export const NAMESPACE_URIS: [ns: string]: string = 
          'svg': 'http://www.w3.org/2000/svg',
          'xhtml': 'http://www.w3.org/1999/xhtml',
          'xlink': 'http://www.w3.org/1999/xlink',
          'xml': 'http://www.w3.org/XML/1998/namespace',
          'xmlns': 'http://www.w3.org/2000/xmlns/',
          ;


          which is used to create element with namespace:



          if (namespace) 
          return document.createElementNS(NAMESPACE_URIS[namespace], name);



          So try using svg key as namespace:



          const path = this.renderer.createElement("path", 'svg')
          ^^^^





          share|improve this answer




















          • Fantastic - that's fixed it.
            – matt helliwell
            Nov 11 '18 at 19:40










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252282%2fdynamically-updating-svg-in-angular%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Angular keeps namespaces map in its code:



          export const NAMESPACE_URIS: [ns: string]: string = 
          'svg': 'http://www.w3.org/2000/svg',
          'xhtml': 'http://www.w3.org/1999/xhtml',
          'xlink': 'http://www.w3.org/1999/xlink',
          'xml': 'http://www.w3.org/XML/1998/namespace',
          'xmlns': 'http://www.w3.org/2000/xmlns/',
          ;


          which is used to create element with namespace:



          if (namespace) 
          return document.createElementNS(NAMESPACE_URIS[namespace], name);



          So try using svg key as namespace:



          const path = this.renderer.createElement("path", 'svg')
          ^^^^





          share|improve this answer




















          • Fantastic - that's fixed it.
            – matt helliwell
            Nov 11 '18 at 19:40















          1














          Angular keeps namespaces map in its code:



          export const NAMESPACE_URIS: [ns: string]: string = 
          'svg': 'http://www.w3.org/2000/svg',
          'xhtml': 'http://www.w3.org/1999/xhtml',
          'xlink': 'http://www.w3.org/1999/xlink',
          'xml': 'http://www.w3.org/XML/1998/namespace',
          'xmlns': 'http://www.w3.org/2000/xmlns/',
          ;


          which is used to create element with namespace:



          if (namespace) 
          return document.createElementNS(NAMESPACE_URIS[namespace], name);



          So try using svg key as namespace:



          const path = this.renderer.createElement("path", 'svg')
          ^^^^





          share|improve this answer




















          • Fantastic - that's fixed it.
            – matt helliwell
            Nov 11 '18 at 19:40













          1












          1








          1






          Angular keeps namespaces map in its code:



          export const NAMESPACE_URIS: [ns: string]: string = 
          'svg': 'http://www.w3.org/2000/svg',
          'xhtml': 'http://www.w3.org/1999/xhtml',
          'xlink': 'http://www.w3.org/1999/xlink',
          'xml': 'http://www.w3.org/XML/1998/namespace',
          'xmlns': 'http://www.w3.org/2000/xmlns/',
          ;


          which is used to create element with namespace:



          if (namespace) 
          return document.createElementNS(NAMESPACE_URIS[namespace], name);



          So try using svg key as namespace:



          const path = this.renderer.createElement("path", 'svg')
          ^^^^





          share|improve this answer












          Angular keeps namespaces map in its code:



          export const NAMESPACE_URIS: [ns: string]: string = 
          'svg': 'http://www.w3.org/2000/svg',
          'xhtml': 'http://www.w3.org/1999/xhtml',
          'xlink': 'http://www.w3.org/1999/xlink',
          'xml': 'http://www.w3.org/XML/1998/namespace',
          'xmlns': 'http://www.w3.org/2000/xmlns/',
          ;


          which is used to create element with namespace:



          if (namespace) 
          return document.createElementNS(NAMESPACE_URIS[namespace], name);



          So try using svg key as namespace:



          const path = this.renderer.createElement("path", 'svg')
          ^^^^






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 19:27









          yurzui

          94.2k11188211




          94.2k11188211











          • Fantastic - that's fixed it.
            – matt helliwell
            Nov 11 '18 at 19:40
















          • Fantastic - that's fixed it.
            – matt helliwell
            Nov 11 '18 at 19:40















          Fantastic - that's fixed it.
          – matt helliwell
          Nov 11 '18 at 19:40




          Fantastic - that's fixed it.
          – matt helliwell
          Nov 11 '18 at 19:40

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252282%2fdynamically-updating-svg-in-angular%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo