I've just written my first angularjs directive, and I was hoping to get some feedback. I already have two doubts myself.
- Why is it that I must use
tElement.append()
to append the canvas element?tElement.html()
doesn't work, at all. That just returns[[object HTMLCanvasElement]]
in plain text inside of the element. - Should I be accessing
canvasElement[0]
? It feels wrong, but it might not be.
Here's the code:
angular.module('yupDraw', [])
.directive('yupCanvas', function factory() {
// Create elements (templates) in vars here
var canvasTemplate = '<canvas></canvas>';
return {
restrict: 'E',
compile: function compile(tElement, tAttrs, transclude) {
var canvasElement = angular.element(canvasTemplate);
tElement.append(canvasElement);
return function link(scope, element, attrs) {
var canvas = new fabric.Canvas(canvasElement[0], {
isDrawingMode: true
});
}
}
};
});
And I'm simply calling the directive like so: <yup-canvas></yup-canvas>
Fire away! :)