Skip to content

Commit 1199b39

Browse files
authored
refactor(material/dialog): use embedded injector to provide ref to template dialogs (#24570)
Previously we had to walk the DOM in order to figure out which dialog ref belonged to a specific template dialog. We no longer need to do that if we provide an injector to the template portal.
1 parent 14f5b6e commit 1199b39

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/material/dialog/dialog-content-directives.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ export class MatDialogActions {
158158
@Input() align?: 'start' | 'center' | 'end' = 'start';
159159
}
160160

161+
// TODO(crisbeto): this utility shouldn't be necessary anymore, because the dialog ref is provided
162+
// both to component and template dialogs through DI. We need to keep it around, because there are
163+
// some internal wrappers around `MatDialog` that happened to work by accident, because we had this
164+
// fallback logic in place.
161165
/**
162166
* Finds the closest MatDialogRef to an element by looking at the DOM.
163167
* @param element Element relative to which to look for a dialog.

src/material/dialog/dialog.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,21 @@ export abstract class _MatDialogBase<C extends _MatDialogContainerBase> implemen
292292
// Create a reference to the dialog we're creating in order to give the user a handle
293293
// to modify and close it.
294294
const dialogRef = new this._dialogRefConstructor(overlayRef, dialogContainer, config.id);
295+
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);
295296

296297
if (componentOrTemplateRef instanceof TemplateRef) {
297298
dialogContainer.attachTemplatePortal(
298-
new TemplatePortal<T>(componentOrTemplateRef, null!, <any>{
299-
$implicit: config.data,
300-
dialogRef,
301-
}),
299+
new TemplatePortal<T>(
300+
componentOrTemplateRef,
301+
null!,
302+
<any>{
303+
$implicit: config.data,
304+
dialogRef,
305+
},
306+
injector,
307+
),
302308
);
303309
} else {
304-
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);
305310
const contentRef = dialogContainer.attachComponentPortal<T>(
306311
new ComponentPortal(
307312
componentOrTemplateRef,

0 commit comments

Comments
 (0)