How to Open and Close Angular-UI popovers programmatically

16,476

Solution 1

First, if you haven't already looked, here are the sources for tooltips and popovers:

tooltip.js

popover.js

You can add custom triggers. Popovers use the $tooltip provider:

.directive( 'popover', [ '$tooltip', function ( $tooltip ) {
  return $tooltip( 'popover', 'popover', 'click' );
}]);

Where the $tooltip's provider $get method, used to make new tooltip's, is defined here:

 this.$get = [ '$window', '$compile', '$timeout', '$parse', '$document', '$position', '$interpolate', function ( $window, $compile, $timeout, $parse, $document, $position, $interpolate ) {
    return function $tooltip ( type, prefix, defaultTriggerShow ) {...}

The $tooltip provider has this method: (triggerMap is the 3 triggers that are defined in the $tooltip provider out of the box.

   /**
   * This allows you to extend the set of trigger mappings available. E.g.:
   *
   *   $tooltipProvider.setTriggers({'openTrigger': 'closeTrigger'});
   */
  this.setTriggers = function setTriggers ( triggers ) {
    angular.extend( triggerMap, triggers );
  };

You can use it in a config block, like this:

myApp.config(['$tooltipProvider', function ( $tooltipProvider ) {
  $tooltipProvider.setTriggers({'openTrigger': 'closeTrigger'}) ;
}]);

Then, you can create a new popover directive like this:

.directive('myPopover', ['$tooltip', function ( $tooltip ) {
  return $tooltip( 'myPopover', 'myPopover', 'openTrigger' );
}]);

And triggering the popover would then be as simple as element.triggerHandler( 'openTrigger' ) (or closeTrigger) where element is the popover.

Solution 2

I extended the popover directive in order to add an attribute "pop-show" which accepts a boolean:

angular.module('app', [ 'ui.bootstrap' ])
  .directive( 'popPopup', function () {
    return {
      restrict: 'EA',
      replace: true,
      scope: { title: '@', content: '@', placement: '@', animation: '&', isOpen: '&' },
      templateUrl: 'template/popover/popover.html'
    };
  })

  .directive('pop', function pop ($tooltip, $timeout) {
    var tooltip = $tooltip('pop', 'pop', 'event');
    var compile = angular.copy(tooltip.compile);
    tooltip.compile = function (element, attrs) {
      var first = true;
      attrs.$observe('popShow', function (val) {
        if (JSON.parse(!first || val || false)) {
          $timeout(function () {
            element.triggerHandler('event');
          });
        }
        first = false;
      });
      return compile(element, attrs);
    };
    return tooltip;
  });

I created a Plunker with an example of how to use this directive:

http://plnkr.co/edit/94ZHgQ?p=preview

Share:
16,476
special0ne
Author by

special0ne

Updated on June 16, 2022

Comments

  • special0ne
    special0ne almost 2 years

    I need to create popovers that gets its content from the server.

    So I created the following directive:

    .directive('myPopover', [myService, function ($myService) {
            return {
                restrict: 'E',
                transclude: true,
                template: '<a href="" ng-click="wordClicked()" class="highlight" popover-trigger="manual" popover="Adequately good for the circumstances" popover-title="good enough " popover-placement="bottom" ng-transclude></a>',
                link: function (scope, element, attrs) {
                    scope.wordClicked = function () {
                        if ( POPUP IS NOT SHOWING ){
                            var message = myService.getMessage({key: element.text()},
                                function () {
                                        console.info("NEED TO SHOW POPOVER WITH "+ message);
                                    });
                        }
                        else {
                            console.info("NEED TO CLOSE POPOVER");
                        }
                    }
                }
            }
        }]);
    

    And inside getMessage success method I need to make the popover to show. The documentation does not give any indication for that though I found comment made By Luthur here it seems like there is a popover-trigger="manual" option. Could not find a way to trigger it programmatically

    Update: I tried to follow Mosho advice but I am having troubles creating a popover with the custom event trigger.

    see plnkr

    Thanks!

    • Grundy
      Grundy over 9 years
      i'm little change your plnkr and all work fine
  • special0ne
    special0ne about 10 years
    thanks for the elaborated answer. I looked at the tooltip code and was able to add my custom triggers. I was not able to create a popover with my trigger as you suggested. See update in post.
  • iLemming
    iLemming over 9 years
    it's a bit confusing... what the first directive does exactly?
  • Grundy
    Grundy over 9 years
    @Agzam it use inside tooltip directive for template popup
  • marcslogic
    marcslogic over 9 years
    What does the template/popover/popover.html file look like? I cannot get it on plunker... Thanks!