Skip to content

Commit 656c681

Browse files
crisbetojelbourn
authored andcommitted
fix(dialog): use view container from config when attaching content (#17819)
Currently we only use the specified `ViewContainerRef` to attach the dialog container, but not the content itself. We couldn't fix this until now due to the bug that was fixed by #17731.
1 parent ad42a1b commit 656c681

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/material/dialog/dialog.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,21 @@ describe('MatDialog', () => {
613613
expect(dialogRef.componentInstance.directionality.value).toBe('ltr');
614614
});
615615

616+
it('should use the passed in ViewContainerRef from the config', fakeAsync(() => {
617+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
618+
viewContainerFixture.detectChanges();
619+
flush();
620+
621+
// One view ref is for the container and one more for the component with the content.
622+
expect(testViewContainerRef.length).toBe(2);
623+
624+
dialogRef.close();
625+
viewContainerFixture.detectChanges();
626+
flush();
627+
628+
expect(testViewContainerRef.length).toBe(0);
629+
}));
630+
616631
it('should close all of the dialogs', fakeAsync(() => {
617632
dialog.open(PizzaMsg);
618633
dialog.open(PizzaMsg);

src/material/dialog/dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ export class MatDialog implements OnDestroy {
257257
if (componentOrTemplateRef instanceof TemplateRef) {
258258
dialogContainer.attachTemplatePortal(
259259
new TemplatePortal<T>(componentOrTemplateRef, null!,
260-
<any>{ $implicit: config.data, dialogRef }));
260+
<any>{$implicit: config.data, dialogRef}));
261261
} else {
262262
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);
263263
const contentRef = dialogContainer.attachComponentPortal<T>(
264-
new ComponentPortal(componentOrTemplateRef, undefined, injector));
264+
new ComponentPortal(componentOrTemplateRef, config.viewContainerRef, injector));
265265
dialogRef.componentInstance = contentRef.instance;
266266
}
267267

0 commit comments

Comments
 (0)