@@ -21,6 +21,7 @@ import {
21
21
Injectable ,
22
22
InjectionToken ,
23
23
Injector ,
24
+ OnDestroy ,
24
25
Optional ,
25
26
SkipSelf ,
26
27
TemplateRef ,
@@ -66,7 +67,7 @@ export const MAT_DIALOG_SCROLL_STRATEGY_PROVIDER = {
66
67
* Service to open Material Design modal dialogs.
67
68
*/
68
69
@Injectable ( )
69
- export class MatDialog {
70
+ export class MatDialog implements OnDestroy {
70
71
private _openDialogsAtThisLevel : MatDialogRef < any > [ ] = [ ] ;
71
72
private readonly _afterAllClosedAtThisLevel = new Subject < void > ( ) ;
72
73
private readonly _afterOpenedAtThisLevel = new Subject < MatDialogRef < any > > ( ) ;
@@ -152,15 +153,7 @@ export class MatDialog {
152
153
* Closes all of the currently-open dialogs.
153
154
*/
154
155
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 ) ;
164
157
}
165
158
166
159
/**
@@ -171,6 +164,12 @@ export class MatDialog {
171
164
return this . openDialogs . find ( dialog => dialog . id === id ) ;
172
165
}
173
166
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
+
174
173
/**
175
174
* Creates the overlay into which the dialog will be loaded.
176
175
* @param config The dialog configuration.
@@ -357,7 +356,19 @@ export class MatDialog {
357
356
}
358
357
}
359
358
}
359
+ }
360
+
361
+ /** Closes all of the dialogs in an array. */
362
+ private _closeDialogs ( dialogs : MatDialogRef < any > [ ] ) {
363
+ let i = dialogs . length ;
360
364
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
+ }
361
372
}
362
373
363
374
}
0 commit comments