I am wondering what the correct angular way to access the main html template element. I can use getelementbyid and also can access it using attr.$$element[0] but wondering if there is a cleaner way. In the example below I would like the equivalent of the getElementById line.
var app = angular.module('test', []);
app.directive('test', function(){
return {
restrict: 'E',
template: '<canvas id="test-canvas"></canvas>',
replace: true,
link: function(scope, elem, attr) {
// what is angular way of doing this?
var canvas = initCanvas(document.getElementById("test-canvas")),
// this works - is this recommended method?
var canvas = attr.$$element[0];
}
};
});
elemparam. It's already a reference to the template root element.elemis a jQuery (or jQuery lite) wrapped DOM element corresponding to the element the directive is operating on. However, it's possible in some cases that this is a copy of the original element, which may or may not be important for your needs. You can get the actual original element in these rare cases via thecompilefunction of the directive.elemis a jQuery wrapped element.elem[0]returns the raw element that has been wrapped. See stackoverflow.com/a/1677910/62082