13

I try to implement a tooltip with angularjs template inside. For this, I use "uib-tooltip-html" and I add an attribute on the element to compile the template. But it doesn't work. Here is the code Here is the plunker http://plnkr.co/edit/y1TvogsFFBoBVra3gO3F?p=preview

   <html>
<head lang="en">
    <meta charset="UTF-8"/>
    <title>uib-tooltip-html test</title>
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.js"></script>
    <script src="https://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular-sanitize.min.js"></script>
    <script>
        var app = angular.module("test", ['ngSanitize','ui.bootstrap']).config(function($sceProvider) {
            $sceProvider.enabled(false);
        });

        app.controller("testController", function($scope, $http, $interval, $sce) {
          $scope.text = $sce.trustAsHtml('<table><tr ng-repeat="x in [1,2,3]"><td>{{ x }}</td></tr></table>');
        });
        app.directive('compileTemplate', function($compile, $parse){
    return {
        link: function(scope, element, attr){
            var parsed = $parse(attr.uibTooltipHtml);
            console.log(attr.uibTooltipHtml);

            function getStringValue() { return (parsed(scope) || '').toString(); }
            console.log(getStringValue())
            //Recompile if the template changes
            scope.$watch(getStringValue, function() {
              console.log('ca passe');
                $compile(element, null, -9999)(scope);  //The -9999 makes it skip directives so that we do not recompile ourselves
            });
        }         
    }
});
    </script>

    </head>
<body>

<div ng-app="test" ng-controller="testController">

    <p style="margin-top: 5em;" uib-tooltip="Some text" >
        A Thing With a Tooltip
    </p>

    <p style="margin-top: 5em;" uib-tooltip-html="text" compile-template>
        A Thing With an HTML Tooltip
    </p>

</div>

Thank you in advance for your answer

1

1 Answer 1

16

You can use uib-tooltip-template like this:

<p style="margin-top: 5em;" uib-tooltip-template="'myTooltipTemplate.html'">
    A Thing With an HTML Tooltip
</p>

And then in put your html in myTooltipTemplate.html:

<table><tr ng-repeat="x in [1,2,3]"><td>{{ x }}</td></tr></table>

The template goes in a separate file.

documentation: https://angular-ui.github.io/bootstrap/#/tooltip

plnkr: http://plnkr.co/edit/tiCHpd0LipixXbO4Xfa5?p=preview

Sign up to request clarification or add additional context in comments.

4 Comments

How would this work with dynamic content? I love that it is simple, but I am not seeing how I could do something like this: <div class="dol-type" uib-tooltip="{{ GetWorkingDays('YTD TY') }}">Actual TY</div> where it will return "Month: 24<br />YTD: 45"?
@Grandizer, the template works just as an "include" of the html to be rendered, so It will work within the context of your div. For example: <div class="dol-type" uib-tooltip-template="'workingdays.html'">ACTUAL TY</div> and you can still call the function in workingdays.html: <div>{{ GetWorkingDays('YTD TY') }}</div>
I guess that is my issue, the "YTD TY" changes. So what variable is the "expression" or $event or something that I can pass through?
You can still use your variable in the template. The template works within the context where it is used. Example: plnkr.co/edit/SpcFxR4Z28ztkIsO3ozW?p=preview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.