Skip to content

Instantly share code, notes, and snippets.

@jbruni
Last active December 23, 2015 11:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jbruni/6629714 to your computer and use it in GitHub Desktop.
Save jbruni/6629714 to your computer and use it in GitHub Desktop.
AngularJS UI Bootstrap "extension" - Popover with bindable HTML template!
<p>Here comes the <b>bindable HTML</b> popover contents!!!</p>
<p>Number: {{number}}</p>
<div ng-init="number = 108">
<button popover-template="example-template.html" class="btn">Example</button>
</div>
<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"></div>
</div>
</div>
angular.module( 'ui.bootstrap.popover' )
.directive( 'popoverTemplatePopup', [ '$templateCache', '$compile', function ( $templateCache, $compile ) {
return {
restrict: 'EA',
replace: true,
scope: { title: '@', content: '@', placement: '@', animation: '&', isOpen: '&' },
templateUrl: 'template/popover/popover-template.html',
link: function( scope, iElement ) {
var content = angular.fromJson( scope.content ),
template = $templateCache.get( content.templateUrl ),
templateScope = scope,
scopeElements = document.getElementsByClassName( 'ng-scope' );
angular.forEach( scopeElements, function( element ) {
var aScope = angular.element( element ).scope();
if ( aScope.$id == content.scopeId ) {
templateScope = aScope;
}
});
iElement.find('div.popover-content').html( $compile( template )( templateScope ) );
}
};
}])
.directive( 'popoverTemplate', [ '$tooltip', function ( $tooltip ) {
var tooltip = $tooltip( 'popoverTemplate', 'popover', 'click' );
tooltip.compile = function() {
return {
'pre': function( scope, iElement, iAttrs ) {
iAttrs.$set( 'popoverTemplate', { templateUrl: iAttrs.popoverTemplate, scopeId: scope.$id } );
},
'post': tooltip.link
};
};
return tooltip;
}]);
@jbruni
Copy link
Author

jbruni commented Sep 19, 2013

While this code works, it contains this horrible "forEach" loop from line 15 to line 20 - necessary to retrieve a scope having only its $id.

So, I can't recommend it.

It would be nice if there was a cleaner way to get the "popoverTemplate" directive scope from inside the "popoverTemplatePopup" directive.

The "glue" between the directives is angular.ui.bootstrap's $tooltip function, which would need to be modified to accomplish an easier way to retrieve the necessary scope.

@jbruni
Copy link
Author

jbruni commented Sep 19, 2013

Worths mentioning that this Gist addresses this issue:

angular-ui/bootstrap#220

feat(popover): Support template URL for partial

@romankolpak
Copy link

Hey there. Wondering if this neat little hacky directive is still functional and tested with the recent versions of angular and ui-bootstrap? I'm trying to incorporate this into my little project but something seems to be broken, because I'm not seeing any popover appearing on the screen, and no errors in the console.

@rinterliche
Copy link

The same here Rawry... Any chance of reproduce it on JSFIDDLE?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment