Rendering SVG templates in AngularJS directives

前端 未结 1 1254
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 16:36

I\'m working on a large project with SVG and angular.js, and need solid support for svg directive templates. Unfortunately when angular renders the templates, it creates DOM

相关标签:
1条回答
  • 2020-12-08 17:13

    I have forked and updated your plunker to make it work here. Your function 'makeNode' was throwing error sometimes when the node in question was detached from document by angular for processing. Using a timeout of 0 ms does the trick of delaying the execution just enough to make the parent node available. Also, I am using the linking function to do all manipulation because the compile function executes only once per ng-repeat but we need it repeated number of times. You can read more about the difference between compile and linking functions under 'The difference between Compile and Link' section on Angular Directive page.

    Here is the relevant code:

    /* Create a shape node with the given settings. */
    function makeNode(name, element, settings) {
      var ns = 'http://www.w3.org/2000/svg';
      var node = document.createElementNS(ns, name);
      for (var attribute in settings) {
        var value = settings[attribute];
        if (value !== null && value !== null && !attribute.match(/\$/) &&
          (typeof value !== 'string' || value !== '')) {
          node.setAttribute(attribute, value);
        }
      }
      return node;
    }
    
    0 讨论(0)
提交回复
热议问题