Using Bootstrap Tooltip with AngularJS

后端 未结 19 1505
抹茶落季
抹茶落季 2020-12-07 10:40

I am trying to use the Bootstrap tooltip in an app of mine. My app is using AngularJS Currently, I have the following:

相关标签:
19条回答
  • 2020-12-07 10:53

    install the dependencies:

    npm install jquery --save
    npm install tether --save
    npm install bootstrap@version --save;
    

    next, add scripts in your angular-cli.json

    "scripts": [
            "../node_modules/jquery/dist/jquery.min.js",
            "../node_modules/tether/dist/js/tether.js",
            "../node_modules/bootstrap/dist/js/bootstrap.min.js",
            "script.js"
        ]
    

    then, create a script.js

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

    now restart your server.

    0 讨论(0)
  • 2020-12-07 10:56

    Only read this if you are assigning tooltips dynamically

    i.e. <div tooltip={{ obj.somePropertyThatMayChange }} ...></div>

    I had an issue with dynamic tooltips that were not always updating with the view. For example, I was doing something like this:

    This didn't work consistently

    <div ng-repeat="person in people">
        <span data-toggle="tooltip" data-placement="top" title="{{ person.tooltip }}">
          {{ person.name }}
        </span> 
    </div> 
    

    And activating it as so:

    $timeout(function() {
      $(document).tooltip({ selector: '[data-toggle="tooltip"]'});
    }, 1500)
    

    However, as my people array would change my tooltips wouldn't always update. I tried every fix in this thread and others with no luck. The glitch seemed to only be happening around 5% of the time, and was nearly impossible to repeat.

    Unfortunately, these tooltips are mission critical for my project, and showing an incorrect tooltip could be very bad.

    What seemed to be the issue

    Bootstrap was copying the value of the title property to a new attribute, data-original-title and removing the title property (sometimes) when I would activate the toooltips. However, when my title={{ person.tooltip }} would change the new value would not always be updated into the property data-original-title. I tried deactivating the tooltips and reactivating them, destroying them, binding to this property directly... everything. However each of these either didn't work or created new issues; such as the title and data-original-title attributes both being removed and un-bound from my object.

    What did work

    Perhaps the most ugly code I've ever pushed, but it solved this small but substantial problem for me. I run this code each time the tooltip is update with new data:

    $timeout(function() {
        $('[data-toggle="tooltip"]').each(function(index) {
          // sometimes the title is blank for no apparent reason. don't override in these cases.
          if ($(this).attr("title").length > 0) {
            $( this ).attr("data-original-title", $(this).attr("title"));
          }
        });
        $timeout(function() {
          // finally, activate the tooltips
          $(document).tooltip({ selector: '[data-toggle="tooltip"]'});
        }, 500);
    }, 1500);
    

    What's happening here in essence is:

    • Wait some time (1500 ms) for the digest cycle to complete, and the titles to be updated.
    • If there's a title property that is not empty (i.e. it has changed), copy it to the data-original-title property so it will be picked up by Bootstrap's toolips.
    • Reactivate the tooltips

    Hope this long answer helps someone who may have been struggling as I was.

    0 讨论(0)
  • 2020-12-07 10:57

    You can create a simple directive like this:

    angular.module('myApp',[])
        .directive('myTooltip', function(){
            return {
                restrict: 'A',
                link: function(scope, element){
                    element.tooltip();
                }
            }
        });
    

    Then, add your custom directive where is necessary:

    <button my-tooltip></button>
    
    0 讨论(0)
  • 2020-12-07 10:58

    var app = angular.module('MyApp', ['mgcrea.ngStrap']);  
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-strap/2.1.2/angular-strap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-strap/2.1.2/angular-strap.tpl.min.js"></script>
    
    <div ng-app="MyApp" class="container" >
    
      <button type="button" 
              class="btn btn-default" 
              data-trigger="hover" 
              data-placement="right"
              data-title="Tooltip on right"
              bs-tooltip>
        MyButton
      </button>
    
    </div>

    0 讨论(0)
  • 2020-12-07 10:59

    The best solution I've been able to come up with is to include an "onmouseenter" attribute on your element like this:

    <button type="button" class="btn btn-default" 
        data-placement="left"
        title="Tooltip on left"
        onmouseenter="$(this).tooltip('show')">
    </button>
    
    0 讨论(0)
  • 2020-12-07 10:59

    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.

    1. Install UI Bootstrap - $ bower install angular-bootstrap

    2. Inject UI Bootstrap as a dependency - angular.module('myModule', ['ui.bootstrap']);

    3. Use the uib-tooltip directive in your html.


    <button class="btn btn-default"
            type="button"
            uib-tooltip="I'm a tooltip!">
         I'm a button!
    </button>
    
    0 讨论(0)
提交回复
热议问题