Skip to content

Commit

Permalink
refactor(material/dialog): use embedded injector to provide ref to te…
Browse files Browse the repository at this point in the history
…mplate 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.

(cherry picked from commit 1199b39)
  • Loading branch information
crisbeto committed Mar 16, 2022
1 parent 1703b83 commit 9c47b2b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/material/dialog/dialog-content-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export class MatDialogContent {}
})
export class MatDialogActions {}

// TODO(crisbeto): this utility shouldn't be necessary anymore, because the dialog ref is provided
// both to component and template dialogs through DI. We need to keep it around, because there are
// some internal wrappers around `MatDialog` that happened to work by accident, because we had this
// fallback logic in place.
/**
* Finds the closest MatDialogRef to an element by looking at the DOM.
* @param element Element relative to which to look for a dialog.
Expand Down
15 changes: 10 additions & 5 deletions src/material/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,21 @@ export abstract class _MatDialogBase<C extends _MatDialogContainerBase> implemen
// Create a reference to the dialog we're creating in order to give the user a handle
// to modify and close it.
const dialogRef = new this._dialogRefConstructor(overlayRef, dialogContainer, config.id);
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);

if (componentOrTemplateRef instanceof TemplateRef) {
dialogContainer.attachTemplatePortal(
new TemplatePortal<T>(componentOrTemplateRef, null!, <any>{
$implicit: config.data,
dialogRef,
}),
new TemplatePortal<T>(
componentOrTemplateRef,
null!,
<any>{
$implicit: config.data,
dialogRef,
},
injector,
),
);
} else {
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);
const contentRef = dialogContainer.attachComponentPortal<T>(
new ComponentPortal(
componentOrTemplateRef,
Expand Down

0 comments on commit 9c47b2b

Please sign in to comment.