Подтвердить что ты не робот

Рендеринг шаблонов SVG в директивах AngularJS

Я работаю над большим проектом с SVG и angular.js и нуждаюсь в твердой поддержке шаблонов директив svg. К сожалению, когда angular отображает шаблоны, он создает узлы DOM, а не узлы SVG. Моя текущая работа заключается в том, чтобы управлять созданием и удалением самих узлов с помощью jquery.svg, но его приближение к нему ограничено. Пример: http://plnkr.co/edit/Xk8wM3?p=preview

Я бы хотел, чтобы директивы element были фактическим элементом svg, а не каким-то искусственным элементом DOM, который на самом деле ничего не делает. Это позволит эффективно использовать фильтры ng-repeat и angular.

Вот plunkr, который нуждается в исправлении: http://plnkr.co/edit/BPvGjf?p=preview

HTML

<svg>
  <!--woot this one works-->
  <shape d="M0,0L250,0L250,250L0,250z" fill="green"></shape>

  <!--nesting directives doesn't work-->
  <group>
    <shape d="M0,0L150,0L150,150L0,150z" fill="red"></shape>
    <shape d="M0,0L100,0L100,100L0,100z" fill="orange"></shape>
  </group>

  <!--ng repeat doesn't work-->
  <shape d="{{square.path}}" fill="{{square.color}}" ng-repeat="square in squares | orderBy:'order'"></shape>
</svg>

Javascript

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.squares = [{
    path: "M0,0L50,0L50,50L0,50z",
    color: 'blue',
    order: 2
  }, {
    path: "M0,0L75,0L75,75L0,75z",
    color: 'purple',
    order: 1
  }];
});

app.directive('group', function($compile) {
  return {
    restrict: 'E',
    transclude: true,
    compile: function(tElement, tAttr, transclude) {
      var path = makeNode('g', tElement, tAttr);

      tElement.append(path.cloneNode(true));

      return function(scope, element) {
        transclude(scope, function(clone, innerScope) {
          element.append($compile(clone)(innerScope));
        })
      }
    }
  }
});

app.directive('shape', function() {
  return {
    restrict: 'E',
    compile: function(tElement, tAttr) {
      var path = makeNode('path', tElement, tAttr);

      tElement.replaceWith(path.cloneNode(true));

      return function(scope, element) {

      }
    }
  }
})

/* Create a shape node with the given settings. */
function makeNode(name, element, settings) {
  var svg = $(element).parents('svg')[0];
  var parent = $(element).parent()[0];
  var factory = $('#factory')[0] || $('body').append('<svg id="factory"></svg>').find('#factory')[0];
  var ns = 'http://www.w3.org/2000/svg';

  // I've tried using the factory here to create the node then clone
  // it into the new svg, but since angular nodes are created child-first, it didn't work

  var node = parent.ownerDocument.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);
    }
  }
  parent.appendChild(node);
  return node;
}
4b9b3361

Ответ 1

Я разветкил и обновил ваш плункер, чтобы он работал здесь. Ваша функция 'makeNode' иногда вызывала ошибку, когда рассматриваемый node был отделен от документа angular для обработки. Использование тайм-аута в 0 мс делает трюк задержки выполнения достаточно, чтобы сделать родительский node доступным. Кроме того, я использую функцию связывания для выполнения всех манипуляций, потому что функция компиляции выполняется только один раз на ng-повтор, но нам это нужно многократно. Вы можете больше узнать о различиях между функциями компиляции и связывания в разделе "Разница между компиляцией и ссылкой" на странице Angular.

Вот соответствующий код:

/* 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;
}