40

I have an AngularJS directive in which I want to add a svg tag to the current element of the directive. Right now I do this with the help of jQuery

link: function (scope, iElement, iAttrs) {

    var svgTag = $('<svg width="600" height="100" class="svg"></svg>');
    $(svgTag).appendTo(iElement[0]);

    ...
    }

Yet I don't want the directive to depend on jQuery for this simple task. How would I accomplish the same with AngularJS means (or native JavaScript code).

4 Answers 4

57

Why not to try simple (but powerful) html() method:

iElement.html('<svg width="600" height="100" class="svg"></svg>');

Or append as an alternative:

iElement.append('<svg width="600" height="100" class="svg"></svg>');

And , of course , more cleaner way:

 var svg = angular.element('<svg width="600" height="100" class="svg"></svg>');
 iElement.append(svg);

Fiddle: http://jsfiddle.net/cherniv/AgGwK/

Sign up to request clarification or add additional context in comments.

4 Comments

thanks that did the trick - new to angularjs ... thanks again
@dorjeduck you welcome , take a look at jsFiddle link that i attached
but it is not appending angular controlls
@MuneemHabib , right it useful for the raw html only. Otherwise you need to use $compile : docs.angularjs.org/api/ng/service/$compile
16

In angularJS, you can use angular.element which is the lite version of jQuery. You can do pretty much everything with it, so you don't need to include jQuery.

So basically, you can rewrite your code to something like this:

link: function (scope, iElement, iAttrs) {
  var svgTag = angular.element('<svg width="600" height="100" class="svg"></svg>');
  angular.element(svgTag).appendTo(iElement[0]);
  //...
}

5 Comments

Thanks a lot nXqd, that does it indeed. (I already accepted Cherniv's answer...) Thanks for pointing this possibility out to me, will be helpful for more complicated stuff I am sure ...
btw, if you need to squeeze every ms, you should use the native function from html, but hopefully it does work cross browser. Btw, you can unaccept it :P [ just kidding ]
angular.element does not have .appendTo docs.angularjs.org/api/ng/function/angular.element
..but it does have append so just switch parent/child situation
For prepending angular.element(svgTag).prependTo(iElement[0]);. Thanks @nXqd
6

You could use something like this

var el = document.createElement("svg");
el.style.width="600px";
el.style.height="100px";
....
iElement[0].appendChild(el)

4 Comments

Thanks pavel for the answer. I tried this yet all I get is "<svg></svg>" in the dom without the width,height attributes (in google chrome)
you need Element.style.width
Thanks Kayo. I just tried el.style.width=600; but still no success, guess I misunderstood your answer
el.style.width = '600px'
2

If your destination element is empty and will only contain the <svg> tag you could consider using ng-bind-html as follow :

Declare your HTML tag in the directive scope variable

link: function (scope, iElement, iAttrs) {

    scope.svgTag = '<svg width="600" height="100" class="svg"></svg>';

    ...
}

Then, in your directive template, just add the proper attribute at the exact place you want to append the svg tag :

<!-- start of directive template code -->
...
<!-- end of directive template code -->
<div ng-bind-html="svgTag"></div>

Don't forget to include ngSanitize to allow ng-bind-html to automatically parse the HTML string to trusted HTML and avoid insecure code injection warnings.

See official documentation for more details.

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.