0

I've developed AngularJS directives to add rectangle into SVG document:

  <svg version="1.1" width="200" height="100">

<g>
  <!-- Works -->
  <g dir-rect1="" viz-settings="settings.rect1" />
  <!-- Doesn't work -->
  <g dir-rect2="" viz-settings="settings.rect2" />
</g>

The only difference is between the directives is that the dir-rect1 directive doesn't use replace and the dir-rect2 directive use:

app.directive('dirRect2', function($compile) {
  return {
    restrict: 'AE',
    scope: {
      vizSettings: '=',
    },
    replace:true, // <--- DOESN'T WORK
    template: '<rect fill="{{vizSettings.fill}}" stroke="{{vizSettings.fill}}" width="{{vizSettings.width}}" height="{{vizSettings.height}}" />',
    link: function($scope, elem, attr, ctrl) {
      console.debug($scope);
    }
  };
});

Why when I use replacing in my dirRect2 directive, I cannot see a rect? It seems that generated code is right in both cases. You can see example on plunker.

1
  • Be careful with replace in angular is deprecated. Commented Dec 23, 2014 at 12:06

2 Answers 2

1

Two things:

  1. replace is deprecated so, I don't recommend you to use it
  2. replace copy the attributes to the children, and that is your problem.

The second directive generates this:

<rect fill="#111111" stroke="#111111" width="10" height="20" dir-rect2="" viz-settings="settings.rect2" class="ng-isolate-scope"></rect>

And it seems that dir-rect2 and viz-settings is breaking the svg in some way... if you remove this attributes with the same code... svg seems to work:

<rect fill="#111111" stroke="#111111" width="10" height="20" ></rect>
Sign up to request clarification or add additional context in comments.

Comments

0

in dirRect1 replace is false (default value) and in dirRect2 is true, so you see a different behavior between the two directives.

The rectangle is not displayed since "rect" tag is required to render the shape in svg! (as I just realized since I never played with svg)

(when you define "replace: true" you are telling Angular to remove the original tag on which the directive is applied)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.