Using Bootstrap Tooltip with AngularJS

后端 未结 19 1504
抹茶落季
抹茶落季 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 11:01

    Because of the tooltip function, you have to tell angularJS that you are using jQuery.

    This is your directive:

    myApp.directive('tooltip', function () {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                element.on('mouseenter', function () {
                    jQuery.noConflict();
                    (function ($) {
                        $(element[0]).tooltip('show');
                    })(jQuery);
                });
            }
        };
    });
    

    and this is how to use the directive :

    
    <a href="#" title="ToolTip!" data-toggle="tooltip" tooltip></a>
    
    0 讨论(0)
  • 2020-12-07 11:07

    Have you included the Bootstrap JS and jQuery?

    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
    

    If you don't already load those, then Angular UI (http://angular-ui.github.io/bootstrap/) may not be much overhead. I use that with my Angular app, and it has a Tooltip directive. Try using tooltip="tiptext"

    <button type="button" class="btn btn-default" 
            data-toggle="tooltip" data-placement="left"
            title="Tooltip on left"
            tooltip="This is a Bootstrap tooltip"
            tooltip-placement="left" >
                Tooltip on left
    </button>
    
    0 讨论(0)
  • 2020-12-07 11:07

    You can use selector option for dynamic single page applications:

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

    if a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to enable dynamic HTML content to have tooltips added.

    0 讨论(0)
  • 2020-12-07 11:07

    Please remember one thing if you want to use bootstrap tooltip in angularjs is order of your scripts if you are using jquery-ui as well, it should be:

    • jQuery
    • jQuery UI
    • Bootstap

    It is tried and tested

    0 讨论(0)
  • 2020-12-07 11:08

    impproving @aStewartDesign answer:

    .directive('tooltip', function(){
      return {
        restrict: 'A',
        link: function(scope, element, attrs){
          element.hover(function(){
            element.tooltip('show');
          }, function(){
            element.tooltip('hide');
          });
        }
      };
    });
    

    There's no need for jquery, its a late anwser but I figured since is the top voted one, I should point out this.

    0 讨论(0)
  • 2020-12-07 11:11

    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:

    $(document).ready(function(){
        $('[data-toggle=tooltip]').hover(function(){
            // on mouseenter
            $(this).tooltip('show');
        }, function(){
            // on mouseleave
            $(this).tooltip('hide');
        });
    });
    

    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:

    app.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 all you have to do is include the "tooltip" attribute on the element you want the tooltip to appear on:

    <a href="#0" title="My Tooltip!" data-toggle="tooltip" data-placement="top" tooltip>My Tooltip Link</a>
    

    Hope that helps!

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