Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI Bootstrap Popover adding a close button

I have the following pop-up and trying to add a close button to be able to close it.

.directive("popoverHtmlUnsafePopup", function () {
  'use strict';
  return {
    restrict: "EA",
    replace: true,
    scope: { title: "@", content: "@", placement: "@", animation: "&", isOpen: "&", manualHide: '&' },
    templateUrl: "views/popover/popover-html-unsafe-popup.html"
  };
})

.directive("popoverHtmlUnsafe", [ '$compile', '$timeout', '$parse', '$window',"$tooltip", function ($compile, $timeout, $parse, $window, $tooltip) {
  'use strict';
  return $tooltip("popoverHtmlUnsafe", "popover", "click");
}]);

<div class="popover {{placement}}" ng-class="{ in: isOpen(), fade: animation() }">
  <div class="arrow"></div>

  <div class="popover-inner">
    <h3 class="popover-title" ng-bind="title" ng-show="title"></h3>
    <div class="popover-content" bind-html-unsafe="content">
    </div>
    <button type="button" class="close" popover-trigger="close">&times;</button>
  </div>
</div>

Just not sure what event or function to call on

<button type="button" class="close" popover-trigger="close">&times;</button>

to be able to close the popup

like image 439
StevieB Avatar asked Feb 03 '15 11:02

StevieB


1 Answers

Ran into issue with this on another project using tooltip. I ended up following some of the patterns in Tooltip.js

By using $compile and a new child scope you can customize this popup how ever you see fit.

.directive('popover2',['$parse', '$compile','$log','$templateCache','$position',
    function ($parse,$compile,$log,$templateCache,$position ) {
         function link(scope, element, attrs) {
             var popupScope = scope.$new(false);   
             var html = $templateCache.get('views/popover/popover-html-unsafe-popup.html').trim();
             var template = angular.element(html)

             var popupLinkFn = $compile(template);

             var popup = popupLinkFn(popupScope);

             popupScope.isOpen = false;             
             popupScope.content = attrs['popover2'];
             var position = function(){
                 var ttPosition = $position.positionElements(element, popup, scope.placement, false);
                 ttPosition.top += 'px';
                 ttPosition.left +='px';
                 popup.css(ttPosition);
             };
             popupScope.setOpen = function(val) {                 

                 popupScope.isOpen = val;
                 popup.css({display: popupScope.isOpen ? 'block' :'none' });       
                 position();
                 // call twice, same as tooltip.js
                 position();

             };            

             var cleanup = element.on('click',function(event){   
                popupScope.setOpen(!popupScope.isOpen);
             });

             element.after(popup);
             element.on('$destroy',cleanup);

         }
        return {
            replace:false,            
            link:link,
            scope: {title: "@", placement: "@",},
        }; 
    }
 ]);

JSFiddle

This directive will allow you to show a popup based on a button and then have a close function. You can extend the show logic how ever you see fit. I have used form valid successfully in the past also.

like image 124
user3420988 Avatar answered Sep 21 '22 19:09

user3420988