From 09e464eab8c6ecbb3132e81f252a22762ea01e38 Mon Sep 17 00:00:00 2001 From: Joe Grund Date: Thu, 30 May 2013 14:06:32 -0400 Subject: [PATCH] fix(tooltip): make sure tooltip scope is evicted from cache. This fix makes sure the tooltip.$scope is cleared from angular.element.cache when $destroy is called, preventing a memory leak. --- src/tooltip/test/tooltip.spec.js | 31 +++++++++++++++++++++++++++++++ src/tooltip/tooltip.js | 2 ++ 2 files changed, 33 insertions(+) diff --git a/src/tooltip/test/tooltip.spec.js b/src/tooltip/test/tooltip.spec.js index 1bc4e2e69b..f429df8b14 100644 --- a/src/tooltip/test/tooltip.spec.js +++ b/src/tooltip/test/tooltip.spec.js @@ -202,6 +202,37 @@ describe('tooltip', function() { })); }); + describe('cleanup', function () { + var elmBody, elm, elmScope, tooltipScope; + + it( 'should not contain a cached reference', inject( function( $compile, $timeout, $rootScope ) { + elmBody = angular.element( + '
' + ); + $compile(elmBody)($rootScope); + $rootScope.$apply(); + elm = elmBody.find('input'); + elmScope = elm.scope(); + tooltipScope = elmScope.$$childTail; + + function inCache() { + var match = false; + + angular.forEach(angular.element.cache, function (item) { + if (item.data && item.data.$scope === tooltipScope) { + match = true; + } + }); + + return match; + } + + expect( inCache() ).toBeTruthy(); + elmScope.$destroy(); + $timeout.flush(); + expect( inCache() ).toBeFalsy(); + })); + }); }); describe( 'tooltipHtmlUnsafe', function() { diff --git a/src/tooltip/tooltip.js b/src/tooltip/tooltip.js index 281d532a4a..8f82c327c5 100644 --- a/src/tooltip/tooltip.js +++ b/src/tooltip/tooltip.js @@ -266,6 +266,8 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position' ] ) element.bind( triggers.hide, hideTooltipBind ); } }); + + scope.$on( '$destroy', hide ); } }; };