I am trying to use the Bootstrap tooltip in an app of mine. My app is using AngularJS Currently, I have the following:
<button type="button" class="btn btn-default"
data-toggle="tooltip" data-placement="left"
title="Tooltip on left">
Tooltip on left
</button>
I think I need to use
$("[data-toggle=tooltip]").tooltip();
However, I'm not sure. Even when I add the line above though, my code doesn't work. I'm trying to avoid using UI bootstrap as it has more than I need. However, if I had to include just the tooltip piece, I'd be open to that. Yet, I can't figure out how to do that.
Can someone show me how to get the Bootstrap Tooltip working with AngularJS?
install the dependencies:
next, add scripts in your angular-cli.json
then, create a script.js
now restart your server.
In order to get the tooltips to work in the first place, you have to initialize them in your code. Ignoring AngularJS for a second, this is how you would get the tooltips to work in jQuery:
This will also work in an AngularJS app so long as it's not content rendered by Angular (eg: ng-repeat). In that case, you need to write a directive to handle this. Here's a simple directive that worked for me:
Then all you have to do is include the "tooltip" attribute on the element you want the tooltip to appear on:
Hope that helps!
Simple Answer - using UI Bootstrap (ui.bootstrap.tooltip)
There seem to be a bunch of very complex answers to this question. Here's what worked for me.
Install UI Bootstrap -
$ bower install angular-bootstrap
Inject UI Bootstrap as a dependency -
angular.module('myModule', ['ui.bootstrap']);
Use the uib-tooltip directive in your html.
You can use
selector
option for dynamic single page applications:easiest way , add
$("[data-toggle=tooltip]").tooltip();
to the concerned controller.I wrote a simple Angular Directive that's been working well for us.
Here's a demo: http://jsbin.com/tesido/edit?html,js,output
Directive (for Bootstrap 3):
Note: If you're using Bootstrap 4, on lines 6 & 11 above you'll need to replace
tooltip('destroy')
withtooltip('dispose')
(Thanks to user1191559 for this upadate)Simply add
bootstrap-tooltip
as an attribute to any element with atitle
. Angular will monitor for changes to thetitle
but otherwise pass the tooltip handling over to Bootstrap.This also allows you to use any of the native Bootstrap Tooltip Options as
data-
attributes in the normal Bootstrap way.Markup:
Clearly this doesn't have all the elaborate bindings & advanced integration that AngularStrap and UI Bootstrap offer, but it's a good solution if you're already using Bootstrap's JS in your Angular app and you just need a basic tooltip bridge across your entire app without modifying controllers or managing mouse events.