Skip to content

Commit ee0cf17

Browse files
committed
fix(tooltip): link on demand
Attempts to fix angular-ui#1450
1 parent 2d5d632 commit ee0cf17

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

Diff for: src/tooltip/test/tooltip.spec.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,12 @@ describe('tooltip', function() {
348348

349349
elm = elmBody.find('input');
350350
elmScope = elm.scope();
351+
elm.trigger('fooTrigger');
351352
tooltipScope = elmScope.$$childTail;
352353
}));
353354

354-
it( 'should not contain a cached reference', function() {
355-
expect( inCache() ).toBeTruthy();
356-
elmScope.$destroy();
357-
expect( inCache() ).toBeFalsy();
358-
});
359-
360355
it( 'should not contain a cached reference when visible', inject( function( $timeout ) {
361356
expect( inCache() ).toBeTruthy();
362-
elm.trigger('fooTrigger');
363357
elmScope.$destroy();
364358
expect( inCache() ).toBeFalsy();
365359
}));

Diff for: src/tooltip/tooltip.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
108108
return {
109109
restrict: 'EA',
110110
scope: true,
111-
link: function link ( scope, element, attrs ) {
112-
var tooltip = $compile( template )( scope );
111+
compile: function (tElem, tAttrs) {
112+
var tooltipLinker = $compile( template );
113+
114+
return function link ( scope, element, attrs ) {
115+
var tooltip;
113116
var transitionTimeout;
114117
var popupTimeout;
115118
var appendToBody = angular.isDefined( options.appendToBody ) ? options.appendToBody : false;
@@ -154,6 +157,10 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
154157
ttHeight,
155158
ttPosition;
156159

160+
// There can only be one tooltip element per directive shown at once.
161+
removeTooltip();
162+
tooltip = tooltipLinker(scope);
163+
157164
// Don't show empty tooltips.
158165
if ( ! scope.tt_content ) {
159166
return;
@@ -234,11 +241,16 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
234241
// need to wait for it to expire beforehand.
235242
// FIXME: this is a placeholder for a port of the transitions library.
236243
if ( scope.tt_animation ) {
237-
transitionTimeout = $timeout(function () {
238-
tooltip.remove();
239-
}, 500);
244+
transitionTimeout = $timeout(removeTooltip, 500);
240245
} else {
246+
removeTooltip();
247+
}
248+
}
249+
250+
function removeTooltip() {
251+
if (tooltip) {
241252
tooltip.remove();
253+
tooltip = null;
242254
}
243255
}
244256

@@ -313,10 +325,9 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
313325
$timeout.cancel( transitionTimeout );
314326
$timeout.cancel( popupTimeout );
315327
unregisterTriggers();
316-
tooltip.remove();
317-
tooltip.unbind();
318-
tooltip = null;
328+
removeTooltip();
319329
});
330+
};
320331
}
321332
};
322333
};

0 commit comments

Comments
 (0)