Create Hoverable popover using angular-ui-bootstrap

后端 未结 11 1033
渐次进展
渐次进展 2020-12-09 10:45

I have the following code for creating a popover in my template file:



        
11条回答
  •  春和景丽
    2020-12-09 10:57

    I think Cosmin has the hoverable popover right, but it does seem to be using the Twitter Bootstrap popover method. The idea is to have this hoverable popover implemented only with AngularJS and one of the Bootstrap wrappers for AngularJS, which are UI Bootstrap or AngularStrap.

    So I have put together an implementation which uses only AngularStrap:

    myApp.directive('hoverablePopover', function ($rootScope, $timeout, $popover) {
        return {
            restrict: "A",
            link: function (scope, element, attrs) {
    
                element.bind('mouseenter', function (e) {
                    $timeout(function () {
                        if (!scope.insidePopover) {
    
                            scope.popover.show();
                            scope.attachEventsToPopoverContent();
                        }
                    }, 200);
                });
    
                element.bind('mouseout', function (e) {
                    $timeout(function () {
                        if (!scope.insidePopover) {
    
                            scope.popover.hide();
                        }
                    }, 400);
                });
    
            },
            controller: function ($scope, $element, $attrs) {
    
                //The $attrs will server as the options to the $popover.
                //We also need to pass the scope so that scope expressions are supported in the  popover attributes
                //like title and content.
                $attrs.scope = $scope;
                var popover = $popover($element, $attrs);
                $scope.popover = popover;
                $scope.insidePopover = false;
    
                $scope.attachEventsToPopoverContent = function () {
    
                    $($scope.popover.$element).on('mouseenter', function () {
    
                        $scope.insidePopover = true;
    
                    });
                    $($scope.popover.$element).on('mouseleave', function () {
    
                        $scope.insidePopover = false;
                        $scope.popover.hide();
    
                    });
                };
            }
        };
    });
    

    When you have a popover element, you need to take into account that you have the element that triggers the popover and you also have the element with the actual popover content.

    The idea is to keep the popover open when you mouse over the element with the actual popover content. In the case of my directive, the link function takes care of the element that triggers the popover and attaches the mouseenter/mouseout event handlers.

    The controller takes care of setting the scope and the popover itself via the AngularStrap $popover service. The controller adds the popover object returned by the AngularStrap service on the scope so that it is available in the link function. It also adds a method attachEventsToPopoverContent, which attaches the mouseenter/mouseout events to the element with the popover content.

    The usage of this directive is like this:

      
    

提交回复
热议问题