Using Bootstrap Tooltip with AngularJS

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

    My Tooltip Link
    

    Hope that helps!

提交回复
热议问题