Angular SVG Path directive?

783 views
Skip to first unread message

Dries De Smet

unread,
Jun 27, 2014, 1:49:26 PM6/27/14
to ang...@googlegroups.com

Bit of a short question, but wondering why this is happening:

In the example posted below, there are two SVG elements, each containing a directive assigned to a element. The first directive doesn't use a template, and is the setup I currently use to assign actions to paths.

The second directive is what I was trying to do in order to clean up my code a bit, but it won't be drawn.

I googled a bit around, and read there is a difference between SVG & DOM nodes? But since both ng-repeat's are working, I'm a bit puzzled on what is going on.

Link to plunkr: http://plnkr.co/edit/cok6O58SOUyaGHG5Jtu5?p=preview

Sander Elias

unread,
Jun 28, 2014, 7:09:45 AM6/28/14
to ang...@googlegroups.com
Hoi Dries,

Svg heeft inderdaad een ander type node nodig, dit is in angular ondersteund vanaf versie 1.3.beta-8!

Groetjes
Sander

Christopher Tang

unread,
Jun 29, 2014, 12:59:12 PM6/29/14
to ang...@googlegroups.com

For those who needed to use the translation bar: it's fixed for > 1.3.beta-8. And to answer your question directly, yes, there is a difference between SVG and HTML nodes in the DOM, as they are defined within different namespaces, although this is more technical than needed to solve the problem. 

The key is the use of the DOM node type = "SVG" as Sander provided in his Plunkr: 

.directive('svgPathReplace', function() {
   return {
      type: 'SVG',
      // snip
   }

If you're interested, you can dig around in the AngularJS source code to understand how DOM nodes are internally created; they are created slightly differently. HTH.

Cheers
Chris

Sander Elias

unread,
Jun 30, 2014, 1:58:55 AM6/30/14
to ang...@googlegroups.com
Hi All,

Sorry, had a slip, and posted a dutch message! Luckily Chris stepped up, ad did an excellent translation of it.
As Chris pointed out, and as shown in my dutch message, there is a new (since 1.3.8) type attribute on the DDO (Directive Definition Object),
This new attribute is by default set to 'html', but it can be changed to 'svg' or 'math' . Those setting are case insensitive. 

Regards
Sander

Robert Grasböck

unread,
Nov 7, 2014, 6:56:29 PM11/7/14
to ang...@googlegroups.com
This example was really, but what about nesting SVG Elements?

e.g.:
<svg-rect ng-repeat="i in items">
   <svg-circle> <svg-circle>
</svg-rect>

I'm searching since days for a valid example

Sander Elias

unread,
Nov 7, 2014, 11:26:51 PM11/7/14
to ang...@googlegroups.com
Hi Robbert,

Can you build a plunk showing your issue? I will have a look at it.

Regards
Sander

Robert Grasböck

unread,
Nov 9, 2014, 3:55:44 PM11/9/14
to ang...@googlegroups.com
Hi Sander!

I made a plunk.
Please review it!
Maybe you can help me with that.

Sander Elias

unread,
Nov 10, 2014, 1:19:02 AM11/10/14
to ang...@googlegroups.com
Hi Robert,

a couple of things. If you use a template directive to replace an existing node, the containing nodes will get removed. 
so you svg-file directive simply replaces the content. the directives inside will never gets displayed.

svg, can only contain valid svg, if there is a content that is not valid svg, it will not get rendered.
svg-rect, and svg-circle are NOT valid svg. In this case you need to use the replace. However, be aware that replace is deprecated.
I don't have the time now to look for a more permanent solution.


Regards
Sander

Robert Grasböck

unread,
Nov 11, 2014, 5:20:47 AM11/11/14
to ang...@googlegroups.com
Hi again!

I have seen some strange constructs like this:
where the inner directives are added while the outer are created.
I found also solutions where the compile service was used to get the inner directives.
But I tried to avoid such code.

For my project I would like to use one svg file (e.g from inkscape) as container.
This SVG should be enriched with icon like elements.

But things always look easier on the paper :)
I created now 2 div tags which lie on top of each other.
Inside the first div tag I put my svg file directive
Insiide the other file I put my icons (like in your example)
It's a little workaround, but it works

Thank you for your time and your help!

regards
Robert

Reply all
Reply to author
Forward
0 new messages