diff --git a/src/material/dialog/dialog.spec.ts b/src/material/dialog/dialog.spec.ts index a4ca37c9b989..efdf384f6fe4 100644 --- a/src/material/dialog/dialog.spec.ts +++ b/src/material/dialog/dialog.spec.ts @@ -613,6 +613,21 @@ describe('MatDialog', () => { expect(dialogRef.componentInstance.directionality.value).toBe('ltr'); }); + it('should use the passed in ViewContainerRef from the config', fakeAsync(() => { + const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef}); + viewContainerFixture.detectChanges(); + flush(); + + // One view ref is for the container and one more for the component with the content. + expect(testViewContainerRef.length).toBe(2); + + dialogRef.close(); + viewContainerFixture.detectChanges(); + flush(); + + expect(testViewContainerRef.length).toBe(0); + })); + it('should close all of the dialogs', fakeAsync(() => { dialog.open(PizzaMsg); dialog.open(PizzaMsg); diff --git a/src/material/dialog/dialog.ts b/src/material/dialog/dialog.ts index de82a839ca41..3500a9a1257b 100644 --- a/src/material/dialog/dialog.ts +++ b/src/material/dialog/dialog.ts @@ -257,11 +257,11 @@ export class MatDialog implements OnDestroy { if (componentOrTemplateRef instanceof TemplateRef) { dialogContainer.attachTemplatePortal( new TemplatePortal(componentOrTemplateRef, null!, - { $implicit: config.data, dialogRef })); + {$implicit: config.data, dialogRef})); } else { const injector = this._createInjector(config, dialogRef, dialogContainer); const contentRef = dialogContainer.attachComponentPortal( - new ComponentPortal(componentOrTemplateRef, undefined, injector)); + new ComponentPortal(componentOrTemplateRef, config.viewContainerRef, injector)); dialogRef.componentInstance = contentRef.instance; }