Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  173] [ 5]  / answers: 1 / hits: 134587  / 11 Years ago, mon, october 21, 2013, 12:00:00

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).


More From » jquery

 Answers
29

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/


[#74846] Saturday, October 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyreese

Total Points: 739
Total Questions: 95
Total Answers: 98

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;