I am an Angular noob, working on an app containing tooltips. Initially, these just contained text. Now we want these to include links, images etc (HTML markup, in other words). The idea is that an element can have two tooltips, and that the HTML one will show up after a while.
I've thrown together a quick and dirty example (FIDDLE) to illustrate the current structure. Here is the directive:
app.directive('myTooltip', ['$timeout', function($timeout) {
return {
restrict: 'A',
link: function($scope, $element, $attr) {
var tooltip;
var richTooltip;
var DELAY = 1200;
var showTooltip = function() {
tooltip.style.visibility = 'visible';
$timeout(showRichTooltip, DELAY);
}
var showRichTooltip = function() {
richTooltip.style.visibility = 'visible';
}
var hideTooltip = function() {
tooltip.style.visibility = 'hidden';
$timeout(hideRichTooltip, DELAY);
}
var hideRichTooltip = function() {
richTooltip.style.visibility = 'hidden';
}
var initTooltips = function() {
tooltip = document.createElement('div');
tooltip.innerHTML = $attr.myTooltip;
tooltip.className = 'tooltip';
tooltip.style.visibility = 'hidden';
document.body.appendChild(tooltip);
$element.on('mouseenter', showTooltip);
$element.on('mouseleave', hideTooltip);
richTooltip = document.createElement('div');
richTooltip.innerHTML = $attr.myRichTooltip;
richTooltip.className = 'rich-tooltip';
richTooltip.style.visibility = 'hidden';
document.body.appendChild(richTooltip);
}
initTooltips();
}
};
}]);
The markup is used kind of like this:
<button my-tooltip="Text only tooltip" my-rich-tooltip="This is a tooltip with <a href='#'>HTML</a>!">Button</button>
Now this works as an example, but it does not look very nice. My question is therefore how one should do this in a nicer way, not having to pass the tooltip HTML as an attribute! It would be nice to, for example, pass an URL containing the HTML instead.
ng-include: docs.angularjs.org/api/ng/directive/ngInclude - you would pass some url as an attribute, and then use that directly in the<ng-include>in your tooltip contenttemplateUrlfor the tooltip, which would point to a template that simply had a<ng-include>(and maybe a few more things)