From 656c6819ab79d8a5d416c2c366178065f4e46d94 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 3 Dec 2019 16:46:26 +0100 Subject: [PATCH] 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. --- src/material/dialog/dialog.spec.ts | 15 +++++++++++++++ src/material/dialog/dialog.ts | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) 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; }