Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 3e5a58e

Browse files
author
Josh David Miller
committed
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.
1 parent c532659 commit 3e5a58e

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/tooltip/test/tooltip.spec.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('tooltip', function() {
1212

1313
beforeEach(inject(function($rootScope, $compile) {
1414
elmBody = angular.element(
15-
'<div><span tooltip="tooltip text">Selector Text</span></div>'
15+
'<div><span tooltip="tooltip text" tooltip-animation="false">Selector Text</span></div>'
1616
);
1717

1818
scope = $rootScope;
@@ -123,6 +123,15 @@ describe('tooltip', function() {
123123
expect(elmBody.children().length).toBe(1);
124124
}));
125125

126+
it( 'should close the tooltip when its trigger element is destroyed', inject( function() {
127+
elm.trigger( 'mouseenter' );
128+
expect( elmScope.tt_isOpen ).toBe( true );
129+
130+
elm.remove();
131+
elmScope.$destroy();
132+
expect( elmBody.children().length ).toBe( 0 );
133+
}));
134+
126135
describe('with specified popup delay', function () {
127136

128137
beforeEach(inject(function ($compile) {

src/tooltip/tooltip.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,24 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position' ] )
267267
}
268268
});
269269

270-
//if a tooltip is attached to <body> we need to remove it on location change
270+
// if a tooltip is attached to <body> we need to remove it on
271+
// location change as its parent scope will probably not be destroyed
272+
// by the change.
271273
if ( options.appendToBody ) {
272-
scope.$on('$locationChangeSuccess', hide);
274+
scope.$on('$locationChangeSuccess', function closeTooltipOnLocationChangeSuccess () {
275+
if ( scope.tt_isOpen ) {
276+
hide();
277+
}
278+
});
273279
}
280+
281+
// if this trigger element is destroyed while the tooltip is open, we
282+
// need to close the tooltip.
283+
scope.$on('$destroy', function closeTooltipOnDestroy () {
284+
if ( scope.tt_isOpen ) {
285+
hide();
286+
}
287+
});
274288
}
275289
};
276290
};

0 commit comments

Comments
 (0)