Using Bootstrap Tooltip with AngularJS

后端 未结 19 1503
抹茶落季
抹茶落季 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:46

    Try the Tooltip (ui.bootstrap.tooltip). See Angular directives for Bootstrap

    <button type="button" class="btn btn-default" tooltip-placement="bottom" uib-tooltip="tooltip message">Test</button>

    It is recommended to avoid JavaScript code on the top of AngularJS

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

    easiest way , add $("[data-toggle=tooltip]").tooltip(); to the concerned controller.

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

    If you're building an Angular app, you can use jQuery, but there is a lot of good reasons to try to avoid it in favor of more angular driven paradigms. You can continue to use the styles provided by bootstrap, but replace the jQuery plugins with native angular by using UI Bootstrap

    Include the Boostrap CSS files, Angular.js, and ui.Bootstrap.js:

    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://code.angularjs.org/1.2.20/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
    

    Make sure you've injected ui.bootstrap when you create your module like this:

    var app = angular.module('plunker', ['ui.bootstrap']);
    

    Then you can use angular directives instead of data attributes picked up by jQuery:

    <button type="button" class="btn btn-default" 
            title="Tooltip on left" data-toggle="tooltip" data-placement="left"
            tooltip="Tooltip on left" tooltip-placement="left" >
      Tooltip on left
    </button>
    

    Demo in Plunker


    Avoiding UI Bootstrap

    jQuery.min.js (94kb) + Bootstrap.min.js (32kb) is also giving you more than you need, and much more than ui-bootstrap.min.js (41kb).

    And time spent downloading the modules is only one aspect of performance.

    If you really wanted to only load the modules you needed, you can "Create a Build" and choose tooltips from the Bootstrap-UI website. Or you can explore the source code for tooltips and pick out what you need.

    Here a minified custom build with just the tooltips and templates (6kb)

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

    You can do this with AngularStrap which

    is a set of native directives that enables seamless integration of Bootstrap 3.0+ into your AngularJS 1.2+ app."

    You can inject the entire library like this:

    var app = angular.module('MyApp', ['mgcrea.ngStrap']); 
    

    Or only pull in the tooltip feature like this:

    var app = angular.module('MyApp', ['mgcrea.ngStrap.tooltip']); 
    

    Demo in Stack Snippets

    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:52

    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):

    // registers native Twitter Bootstrap 3 tooltips
    app.directive('bootstrapTooltip', function() {
      return function(scope, element, attrs) {
        attrs.$observe('title',function(title){
          // Destroy any existing tooltips (otherwise new ones won't get initialized)
          element.tooltip('destroy');
          // Only initialize the tooltip if there's text (prevents empty tooltips)
          if (jQuery.trim(title)) element.tooltip();
        })
        element.on('$destroy', function() {
          element.tooltip('destroy');
          delete attrs.$$observers['title'];
        });
      }
    });
    

    Note: If you're using Bootstrap 4, on lines 6 & 11 above you'll need to replace tooltip('destroy') with tooltip('dispose') (Thanks to user1191559 for this upadate)

    Simply add bootstrap-tooltip as an attribute to any element with a title. Angular will monitor for changes to the title 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:

    <div bootstrap-tooltip data-placement="left" title="Tooltip on left">
            Tooltip on left
    </div>
    

    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.

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

    AngularStrap doesn't work in IE8 with angularjs version 1.2.9 so not use this if your application needs to support IE8

    0 讨论(0)
提交回复
热议问题