From 0754ad7b5c7ef7b018d0caebdb8799f24bd0cd8c Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Mon, 30 Dec 2013 15:53:09 -0800 Subject: [PATCH] fix(modal): leaking watchers due to scope re-use Previously, the backdropScope was being re-used and each time it was linked, it would attach more watchers to the scope. Closes #1491 Closes #1498 --- src/modal/modal.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index adcc95fe65..f04e02d58d 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -110,8 +110,7 @@ angular.module('ui.bootstrap.modal', []) var OPENED_MODAL_CLASS = 'modal-open'; - var backdropjqLiteEl, backdropDomEl; - var backdropScope = $rootScope.$new(true); + var backdropDomEl, backdropScope; var openedWindows = $$stackedMap.createNew(); var $modalStack = {}; @@ -127,7 +126,9 @@ angular.module('ui.bootstrap.modal', []) } $rootScope.$watch(backdropIndex, function(newBackdropIndex){ - backdropScope.index = newBackdropIndex; + if (backdropScope) { + backdropScope.index = newBackdropIndex; + } }); function removeModalWindow(modalInstance) { @@ -146,6 +147,9 @@ angular.module('ui.bootstrap.modal', []) if (backdropDomEl && backdropIndex() == -1) { backdropDomEl.remove(); backdropDomEl = undefined; + + backdropScope.$destroy(); + backdropScope = undefined; } //destroy scope @@ -174,12 +178,14 @@ angular.module('ui.bootstrap.modal', []) keyboard: modal.keyboard }); - var body = $document.find('body').eq(0); + var body = $document.find('body').eq(0), + currBackdropIndex = backdropIndex(); - if (backdropIndex() >= 0 && !backdropDomEl) { - backdropjqLiteEl = angular.element('
'); - backdropDomEl = $compile(backdropjqLiteEl)(backdropScope); - body.append(backdropDomEl); + if (currBackdropIndex >= 0 && !backdropDomEl) { + backdropScope = $rootScope.$new(true); + backdropScope.index = currBackdropIndex; + backdropDomEl = $compile('
')(backdropScope); + body.append(backdropDomEl); } var angularDomEl = angular.element('
');