diff --git a/src/tooltip/test/tooltip.spec.js b/src/tooltip/test/tooltip.spec.js
index c068377f17..a9b762c6a9 100644
--- a/src/tooltip/test/tooltip.spec.js
+++ b/src/tooltip/test/tooltip.spec.js
@@ -12,7 +12,7 @@ describe('tooltip', function() {
beforeEach(inject(function($rootScope, $compile) {
elmBody = angular.element(
- '
Selector Text
'
+ 'Selector Text
'
);
scope = $rootScope;
@@ -123,6 +123,15 @@ describe('tooltip', function() {
expect(elmBody.children().length).toBe(1);
}));
+ it( 'should close the tooltip when its trigger element is destroyed', inject( function() {
+ elm.trigger( 'mouseenter' );
+ expect( elmScope.tt_isOpen ).toBe( true );
+
+ elm.remove();
+ elmScope.$destroy();
+ expect( elmBody.children().length ).toBe( 0 );
+ }));
+
describe('with specified popup delay', function () {
beforeEach(inject(function ($compile) {
diff --git a/src/tooltip/tooltip.js b/src/tooltip/tooltip.js
index 1e137f97bf..2d4f85b4fa 100644
--- a/src/tooltip/tooltip.js
+++ b/src/tooltip/tooltip.js
@@ -267,10 +267,24 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position' ] )
}
});
- //if a tooltip is attached to we need to remove it on location change
+ // if a tooltip is attached to we need to remove it on
+ // location change as its parent scope will probably not be destroyed
+ // by the change.
if ( options.appendToBody ) {
- scope.$on('$locationChangeSuccess', hide);
+ scope.$on('$locationChangeSuccess', function closeTooltipOnLocationChangeSuccess () {
+ if ( scope.tt_isOpen ) {
+ hide();
+ }
+ });
}
+
+ // if this trigger element is destroyed while the tooltip is open, we
+ // need to close the tooltip.
+ scope.$on('$destroy', function closeTooltipOnDestroy () {
+ if ( scope.tt_isOpen ) {
+ hide();
+ }
+ });
}
};
};