From 081dd896e30511add2b37d6f95d25cac388bae47 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Wed, 3 Jul 2019 16:43:22 +0200 Subject: [PATCH] feat(dialog): allow for custom ComponentFactoryResolver to be passed in Allows for a different `ComponentFactoryResolver` to be passed in when creating a dialog. Fixes #16431. --- src/material/dialog/dialog-config.ts | 5 ++++- src/material/dialog/dialog.spec.ts | 16 +++++++++++++++- src/material/dialog/dialog.ts | 4 ++-- tools/public_api_guard/material/dialog.d.ts | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/material/dialog/dialog-config.ts b/src/material/dialog/dialog-config.ts index b99863d1ebb3..d1db9facf555 100644 --- a/src/material/dialog/dialog-config.ts +++ b/src/material/dialog/dialog-config.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {ViewContainerRef} from '@angular/core'; +import {ViewContainerRef, ComponentFactoryResolver} from '@angular/core'; import {Direction} from '@angular/cdk/bidi'; import {ScrollStrategy} from '@angular/cdk/overlay'; @@ -114,5 +114,8 @@ export class MatDialogConfig { */ closeOnNavigation?: boolean = true; + /** Alternate `ComponentFactoryResolver` to use when resolving the associated component. */ + componentFactoryResolver?: ComponentFactoryResolver; + // TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling. } diff --git a/src/material/dialog/dialog.spec.ts b/src/material/dialog/dialog.spec.ts index 5093fc868e04..f7201a5417af 100644 --- a/src/material/dialog/dialog.spec.ts +++ b/src/material/dialog/dialog.spec.ts @@ -16,7 +16,8 @@ import { NgModule, TemplateRef, ViewChild, - ViewContainerRef + ViewContainerRef, + ComponentFactoryResolver } from '@angular/core'; import {By} from '@angular/platform-browser'; import {NoopAnimationsModule} from '@angular/platform-browser/animations'; @@ -741,6 +742,19 @@ describe('MatDialog', () => { expect(scrollStrategy.enable).toHaveBeenCalled(); })); + it('should be able to pass in an alternate ComponentFactoryResolver', + inject([ComponentFactoryResolver], (resolver: ComponentFactoryResolver) => { + spyOn(resolver, 'resolveComponentFactory').and.callThrough(); + + dialog.open(PizzaMsg, { + viewContainerRef: testViewContainerRef, + componentFactoryResolver: resolver + }); + viewContainerFixture.detectChanges(); + + expect(resolver.resolveComponentFactory).toHaveBeenCalled(); + })); + describe('passing in data', () => { it('should be able to pass in data', () => { let config = { diff --git a/src/material/dialog/dialog.ts b/src/material/dialog/dialog.ts index baabadabcb7c..758e17470f81 100644 --- a/src/material/dialog/dialog.ts +++ b/src/material/dialog/dialog.ts @@ -223,8 +223,8 @@ export class MatDialog implements OnDestroy { const injector = new PortalInjector(userInjector || this._injector, new WeakMap([ [MatDialogConfig, config] ])); - const containerPortal = - new ComponentPortal(MatDialogContainer, config.viewContainerRef, injector); + const containerPortal = new ComponentPortal(MatDialogContainer, + config.viewContainerRef, injector, config.componentFactoryResolver); const containerRef = overlay.attach(containerPortal); return containerRef.instance; diff --git a/tools/public_api_guard/material/dialog.d.ts b/tools/public_api_guard/material/dialog.d.ts index dfdf61450454..ef746fd688a1 100644 --- a/tools/public_api_guard/material/dialog.d.ts +++ b/tools/public_api_guard/material/dialog.d.ts @@ -61,6 +61,7 @@ export declare class MatDialogConfig { autoFocus?: boolean; backdropClass?: string; closeOnNavigation?: boolean; + componentFactoryResolver?: ComponentFactoryResolver; data?: D | null; direction?: Direction; disableClose?: boolean;