Skip to content

Commit b0a9fde

Browse files
committed
fix(dialog): leaking MdDialogContainer references
Fixes an issue that caused the `MdDialogContainer` references to not be cleaned up, and as a result, any of the `MdDialogRef` and `MdDialogConfig` instances as well. The issue seems to come from the fact that a couple of blocks that look like: ``` this._ngZone.onMicrotaskEmpty.first().subscribe(() => { this._elementFocusedBeforeDialogWasOpened = document.activeElement; this._focusTrap.focusFirstTabbableElement(); }); ``` End up being transpiled to: ``` var _this = this; this._ngZone.onMicrotaskEmpty.first().subscribe(function () { _this._elementFocusedBeforeDialogWasOpened = document.activeElement; _this._focusTrap.focusFirstTabbableElement(); }); ``` This seems to cause the browser to retain the `_this` reference after the dialog has been destroyed. Fixes angular#2876.
1 parent c203589 commit b0a9fde

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/lib/dialog/dialog-container.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
109109
// If were to attempt to focus immediately, then the content of the dialog would not yet be
110110
// ready in instances where change detection has to run first. To deal with this, we simply
111111
// wait for the microtask queue to be empty.
112-
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
113-
this._elementFocusedBeforeDialogWasOpened = document.activeElement as HTMLElement;
114-
this._focusTrap.focusFirstTabbableElement();
115-
});
112+
this._elementFocusedBeforeDialogWasOpened = document.activeElement as HTMLElement;
113+
this._focusTrap.focusFirstTabbableElementWhenReady();
116114
}
117115

118116
/**
@@ -137,16 +135,15 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
137135
// the dialog was opened. Wait for the DOM to finish settling before changing the focus so
138136
// that it doesn't end up back on the <body>. Also note that we need the extra check, because
139137
// IE can set the `activeElement` to null in some cases.
140-
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
141-
let toFocus = this._elementFocusedBeforeDialogWasOpened as HTMLElement;
138+
let toFocus = this._elementFocusedBeforeDialogWasOpened as HTMLElement;
139+
let animationStream = this._onAnimationStateChange;
142140

143-
// We need to check whether the focus method exists at all, because IE seems to throw an
144-
// exception, even if the element is the document.body.
141+
this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
145142
if (toFocus && 'focus' in toFocus) {
146143
toFocus.focus();
147144
}
148145

149-
this._onAnimationStateChange.complete();
146+
animationStream.complete();
150147
});
151148
}
152149
}

0 commit comments

Comments
 (0)