Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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! :)

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.