Show Bootstrap tooltip in AngularJS ng-repeat elements

  Pi Ke        2016-05-21 10:52:02       15,847        6    

In contemporary web application development, many front-end frameworks have been used to accelerate the speed of development and circumvent browser compatibility issues. Among them, AngularJS and Bootstrap are two frequently used. 

AngularJS is a MVC JavaScript framework developed by Google to provide easy synchronization between user view and data model. While Bootstrap is developed by Twitter and it eases the work of designing a simple and concise UI without much manual design work involved. In some web applications, both frameworks are used together.

Tooltip is a component provided by Bootstrap which offers a smooth feel of displaying tooltip while hovering on some elements on a page, for example on a button or on a link. Usually, to enable tooltip functionality, below attributes need to be added to an element。

  • data-toggle = "tooltip"
  • data-placement = "right" (Direction can be left, right, top, bottom)
  • title = "" (Text on tooltip)

For example, to display "Edit" on a button, below is the HTML code.

<button class="btn btn-default" 
		data-toggle="tooltip" 
		data-placement="right" 
		title="Edit"
>
	EDIT
</button>

And in JavaScript, initialize the tooltip with below code :

$('[data-toggle="tooltip"]').tooltip(); 

Now what if you want to achieve the same in ng-repeat elements while using AngularJS? If you just copy and paste above HTML code into the ng-repeat elements, you would be disappointed to see that the tooltip will not show up anymore.  In this case, a new directive can be created to bring back the tooltip.

A directive extends the standard HTML capability with customized markers. This is one of the most important and powerful features of AngularJS. To add the tooltip capability to an AngularJS-initialized element, below directive can be created.

// ToolTipApp is the ng-app application in your web app
ToolTipApp.directive('tooltip', function(){
    return {
        restrict: 'A',
        link: function(scope, element, attrs){
            $(element).hover(function(){
                // on mouseenter
                $(element).tooltip('show');
            }, function(){
                // on mouseleave
                $(element).tooltip('hide');
            });
        }
    };
});

Then in the element you want to add the tooltip, you need to add an attribute tooltip. With this, AngularJS will adds the extended behavior you defined to the element.

<button class="btn btn-default" 
		data-toggle="tooltip" 
		data-placement="right" 
		title="Edit"
		tooltip>
	EDIT
</button>

What the directive does is it will match all attributes with name tooltip and register the hover listener on the element containing this attribute. Now the tooltiip will just work as normal. 

ANGULARJS  NG-REPEAT  BOOTSTRAP  TOOLTIP 

       

  RELATED


  6 COMMENTS


Anonymous [Reply]@ 2016-12-19 04:41:50

 Thank you so much for this. Spent the whole day looking for the problem and realized only those ng-repeat elements have their tooltip not working. This solution worked like a charm! =)

Anonymous [Reply]@ 2017-02-18 05:17:18

dsadasd

Anonymous [Reply]@ 2020-02-24 19:47:31

<div ng-repeat="day in ::c.data.days" class="day" ng-class="{'multiple-issues': (day.count > 0)}" ng-style="::{'background-color': day.color}" tooltips tooltip-template="{{::day.msg}} {{::day.date}}">

 

This line is not doing the showing anything when hovering. help?

Anonymous [Reply]@ 2020-07-04 07:33:27

I have same problem in bootstrap 5 with angularjs 1.8.x. whenever i hover the mouse on element only title show me in black background white text but not as looks tooltip.. so how i fixed this issue..please help and above code not working too in my case

A.Awd [Reply]@ 2020-09-28 03:28:55

thanks a lot worked fine ^_^

Ke Pi [Reply]@ 2020-10-02 05:06:55

Very glad to hear about this



  RANDOM FUN

A sad outsourcing story