There are a few things you can do to get the element of a directive.
Element on event
If you need to pass the element on an event, you can create a callback which can pass the element back. If you do not need a reference to it all the time, this is the preferred method.
In you return object in the directive, Add something like
scope:{
elementclicked: "&"
}
In the template of your directive, you can add
<....... ng-click="ElementClicked(event)"........>
In your Directive Controller, you can now handle the click and Pass the results
$scope.ElementClicked = function ($event) {
if ($scope.elementclicked != undefined) {
elementclicked({ event: $event });
}
}
Now you pass your callback like any other to the directive.
<yourDirective elementclicked="MyFunction(event)" ....>
Element when linked
If you need a reference at the time of creation, you can do that as well. If you Pass in a data structure such as settings you could set it in the linking event. When you do your linking in the directive, just set the element.
scope:{
settings:"="
},
link:function(scope,element){
scope.$watch('settings',function(){
if(scope.settings!=undefined){
scope.settings.element=element;
}
}
}
This will watch for when the settings are bound and set the element property. The big disadvantage here is that you are appending a property to a passed object but if it is for a directive inside a directive or it is just your project, it should be fine.
Another way to do it would be to use the first method and create an elementlinked(element) callback and fire it after you link the scope.