From 3e5a58e54cda36f3171bf5463646876c1c44a81a Mon Sep 17 00:00:00 2001 From: Josh David Miller Date: Thu, 9 May 2013 09:24:10 -0700 Subject: [PATCH] fix(tooltip): tooltips will hide on scope.$destroy When an element on which the tooltip is applied is destroyed (along with its scope), the tooltip popup will now be closed if it was open. Also refactored the $locationChangeSuccess binding as well to not waste a run of `hide()` unless the tooltip was already open, following the same pattern used in this bug fix. Closes #410. --- src/tooltip/test/tooltip.spec.js | 11 ++++++++++- src/tooltip/tooltip.js | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) 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(); + } + }); } }; };