Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 + Bootstrap Tooltip Not Working

What am I doing wrong?

I'm trying to get a Bootstrap Tooltip to work in Angular 5. We have the Javascript library set up via the footer of the index.html page, as recommended. The documents on Bootstrap use JQuery to get ahold of the elements, then call the tooltip() function on them.

Thinking I might be able to do the same, but using the getElementById function to obtain a handle on the button, I wrote the following (the button is the item that has a tooltip defined on it):

  ngAfterViewInit(){
    let button = document.getElementById('tooltipBtn');
    button.tooltip();
  }

Template code (separate file):

<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom" id="tooltipBtn">
  Tooltip on bottom
</button>

The error I get (in the browser console):

ERROR
Error: button.tooltip is not a function
like image 594
theMayer Avatar asked May 03 '18 22:05

theMayer


People also ask

How do I enable tooltip in bootstrap 5?

To create a tooltip, add the data-bs-toggle="tooltip" attribute to an element. Note: Tooltips must be initialized with JavaScript to work.

How do I change dynamic value tooltip?

Here's how: Drag the calculated field to the appropriate tooltip and you'll see an ATTR dimension pill with a tooltip logo in the Marks card. Insert the ATTR budget and adjusted inflated gross calculated fields into its corresponding tooltip as seen in Image 6. After that, you're dynamic tooltip should work!

What are the ways to enable Bootstrap Tooltips plugin for the HTML elements?

To create a tooltip, add the data-toggle="tooltip" attribute to an element. Note: Tooltips must be initialized with jQuery: select the specified element and call the tooltip() method.

How do I change the tooltip background color in bootstrap 5?

You can add different Bootstrap 5 tooltip directions by changing top , right , left , bottom in the data-bs-palcement . By aiming . tooltip-inner and . tooltip-arrow::before , you can change the color.


2 Answers

You can use ng-bootstrap

npm install --save @ng-bootstrap/ng-bootstrap

Import NgbModule in your app module

import {NgbModule} from '@ng-bootstrap/ng-bootstrap'; 

 imports: [
    NgbModule.forRoot(),
  ],

HTML:

 <button class="btn btn-secondary" placement="bottom" ngbTooltip="Tooltip Text..!!">
like image 81
anud33p Avatar answered Oct 17 '22 05:10

anud33p


So, there are a few things that I had to do to get this to work properly.

Angular.JSON (Angular 6)

In this file, it is necessary to put in the Javascript and CSS links for bootstrap. The file used to be known as .angular-cli.json for previous versions, and it's one level deeper, so if you have that file, you'll need to make it ../node_modules. In any case, you'll have something like the following:

  "styles": [
   "src/styles.scss",
   "node_modules/bootstrap/dist/css/bootstrap.min.css"
  ],
  "scripts": [
   "../node_modules/jquery/dist/jquery.js",
   "../node_modules/tether/dist/js/popper.js",
   "../node_modules/bootstrap/dist/js/bootstrap.js"
  ]

This will load the appropriate CSS and Javascript into your webpack, so that it will be globally accessible. It's very important to load them in the correct order, as shown. Variations will fail. More detailed instructions can be found here.

*.component.ts

Within each of your components, to use the $ operator, all you have to do is, at the top of the file, type

declare var $;

That's it. You won't get autocomplete, but the application will function when it's compiled.

Working with other JS Libraries

This also applies to, for example, lodash (the _ operator) and many other Javascript libraries. It's also possible with many libraries to use, for example,

import * as $ from 'jquery' - however, for some reason, I have not found that to be reliable with the jquery library itself. It has worked for many others, including moment.js and shortid.

like image 22
theMayer Avatar answered Oct 17 '22 05:10

theMayer