@@ -21,6 +21,7 @@ import {
2121 Injectable ,
2222 InjectionToken ,
2323 Injector ,
24+ OnDestroy ,
2425 Optional ,
2526 SkipSelf ,
2627 TemplateRef ,
@@ -66,7 +67,7 @@ export const MAT_DIALOG_SCROLL_STRATEGY_PROVIDER = {
6667 * Service to open Material Design modal dialogs.
6768 */
6869@Injectable ( )
69- export class MatDialog {
70+ export class MatDialog implements OnDestroy {
7071 private _openDialogsAtThisLevel : MatDialogRef < any > [ ] = [ ] ;
7172 private readonly _afterAllClosedAtThisLevel = new Subject < void > ( ) ;
7273 private readonly _afterOpenedAtThisLevel = new Subject < MatDialogRef < any > > ( ) ;
@@ -152,15 +153,7 @@ export class MatDialog {
152153 * Closes all of the currently-open dialogs.
153154 */
154155 closeAll ( ) : void {
155- let i = this . openDialogs . length ;
156-
157- while ( i -- ) {
158- // The `_openDialogs` property isn't updated after close until the rxjs subscription
159- // runs on the next microtask, in addition to modifying the array as we're going
160- // through it. We loop through all of them and call close without assuming that
161- // they'll be removed from the list instantaneously.
162- this . openDialogs [ i ] . close ( ) ;
163- }
156+ this . _closeDialogs ( this . openDialogs ) ;
164157 }
165158
166159 /**
@@ -171,6 +164,12 @@ export class MatDialog {
171164 return this . openDialogs . find ( dialog => dialog . id === id ) ;
172165 }
173166
167+ ngOnDestroy ( ) {
168+ // Only close the dialogs at this level on destroy
169+ // since the parent service may still be active.
170+ this . _closeDialogs ( this . _openDialogsAtThisLevel ) ;
171+ }
172+
174173 /**
175174 * Creates the overlay into which the dialog will be loaded.
176175 * @param config The dialog configuration.
@@ -357,7 +356,19 @@ export class MatDialog {
357356 }
358357 }
359358 }
359+ }
360+
361+ /** Closes all of the dialogs in an array. */
362+ private _closeDialogs ( dialogs : MatDialogRef < any > [ ] ) {
363+ let i = dialogs . length ;
360364
365+ while ( i -- ) {
366+ // The `_openDialogs` property isn't updated after close until the rxjs subscription
367+ // runs on the next microtask, in addition to modifying the array as we're going
368+ // through it. We loop through all of them and call close without assuming that
369+ // they'll be removed from the list instantaneously.
370+ dialogs [ i ] . close ( ) ;
371+ }
361372 }
362373
363374}
0 commit comments