I am using AngularJS and I want to create a button that does an action only if the user is registered. If the user is not registered, it opens a signup tooltip with a specific message. Here is the button markup:
<button signup-tooltip="submitForm()" signup-message="Please sign in to submit the form">submit form</button>
The signup tooltip template looks like this:
<div class="signup-tooltip-view">
....Many elements
....
</div>
Important thing is that the signup tooltip must opened above the button.
My signupTooltip looks like that:
module.directive('signupTooltip', function() {
return {
scope: {
action: '&signupTooltip',
message: '@signupMessage'
},
templateUrl: 'path/to/signup/tooltip.html',
link: function(scope, element, attrs) {
function calculatePosition() {
// This method calculates the position of the tooltip (above element)
var top, left;
....
....
....
return {
top: top,
left: left
};
}
element.click(function() {
if (user.isRegistered) {
scope.action();
} else {
var tooltipPosition = calculatePosition();
// Here I need a reference for the template element <------PROBLEM
var tooltipTemplate = ..... HOW TO GET THE TOOLTIP ELEMENT
tooltipTemplate
.appendTo(element.parent())
.css(tooltipPosition);
}
});
}
};
});
Inside the directive I need a reference for the compiled element (the element that created from). How can I get it (keep in mind that I need signupMessage to be compiled with the template)??
What is the angular way for such use case (tooltips)?