0

I'm trying make a directive out of a great jquery zooming/panning library. How can I access the elements in my template to initialize the plugin?

The directive looks like this as of now:

    directive('zui', [function () {
    return {
        restrict: 'E',
        scope: { URL: "@"},
        template: '<div id="zui" ><div id="viewport" ><img ng-src="{{imageURL}}"></div></div>',
        link: function (scope, element, attrs) {
            scope.imageURL = URL;

            var zui = new ZUI53.Viewport( document.getElementById('zui') );
            zui.addSurface( new ZUI53.Surfaces.CSS( document.getElementById('viewport') ) );

            var pan_tool = new ZUI53.Tools.Pan(zui);
            zui.toolset.add( pan_tool );
            pan_tool.attach()
        }
    };
}]);

Clearly document.getByID() is not the best way to accomplish this. What is a better solution? Thanks a lot.

bornytm
  • 793
  • 1
  • 11
  • 27

1 Answers1

1

Getting to the element using document.getElementById('zui') is indeed not nice. If you ever had to have two instances of the zui directive, it would break.

  1. Don't use id on the root element. Use a marker class such as class="zui" or a data attribute such as data-zui.

  2. Using jquery, you can get to your element using find like this: element.find('.zui')[0];.

Sylvain
  • 19,099
  • 23
  • 96
  • 145