我想知道访问主 html 模板元素的正确角度方式是什么。我可以使用 getelementbyid 也可以使用 attr.$$element[0] 访问它,但想知道是否有更清洁的方法。在下面的示例中,我想要 getElementById 行的等效项。
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];
}
};
});