Skip to content

Commit 565bd7d

Browse files
crisbetojelbourn
authored andcommitted
feat(dialog): allow for custom ComponentFactoryResolver to be passed in (#16437)
Allows for a different `ComponentFactoryResolver` to be passed in when creating a dialog. Fixes #16431.
1 parent a415b52 commit 565bd7d

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/material/dialog/dialog-config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ViewContainerRef} from '@angular/core';
9+
import {ViewContainerRef, ComponentFactoryResolver} from '@angular/core';
1010
import {Direction} from '@angular/cdk/bidi';
1111
import {ScrollStrategy} from '@angular/cdk/overlay';
1212

@@ -114,5 +114,8 @@ export class MatDialogConfig<D = any> {
114114
*/
115115
closeOnNavigation?: boolean = true;
116116

117+
/** Alternate `ComponentFactoryResolver` to use when resolving the associated component. */
118+
componentFactoryResolver?: ComponentFactoryResolver;
119+
117120
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
118121
}

src/material/dialog/dialog.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
NgModule,
1717
TemplateRef,
1818
ViewChild,
19-
ViewContainerRef
19+
ViewContainerRef,
20+
ComponentFactoryResolver
2021
} from '@angular/core';
2122
import {By} from '@angular/platform-browser';
2223
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
@@ -741,6 +742,19 @@ describe('MatDialog', () => {
741742
expect(scrollStrategy.enable).toHaveBeenCalled();
742743
}));
743744

745+
it('should be able to pass in an alternate ComponentFactoryResolver',
746+
inject([ComponentFactoryResolver], (resolver: ComponentFactoryResolver) => {
747+
spyOn(resolver, 'resolveComponentFactory').and.callThrough();
748+
749+
dialog.open(PizzaMsg, {
750+
viewContainerRef: testViewContainerRef,
751+
componentFactoryResolver: resolver
752+
});
753+
viewContainerFixture.detectChanges();
754+
755+
expect(resolver.resolveComponentFactory).toHaveBeenCalled();
756+
}));
757+
744758
describe('passing in data', () => {
745759
it('should be able to pass in data', () => {
746760
let config = {

src/material/dialog/dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ export class MatDialog implements OnDestroy {
223223
const injector = new PortalInjector(userInjector || this._injector, new WeakMap([
224224
[MatDialogConfig, config]
225225
]));
226-
const containerPortal =
227-
new ComponentPortal(MatDialogContainer, config.viewContainerRef, injector);
226+
const containerPortal = new ComponentPortal(MatDialogContainer,
227+
config.viewContainerRef, injector, config.componentFactoryResolver);
228228
const containerRef = overlay.attach<MatDialogContainer>(containerPortal);
229229

230230
return containerRef.instance;

tools/public_api_guard/material/dialog.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export declare class MatDialogConfig<D = any> {
6161
autoFocus?: boolean;
6262
backdropClass?: string;
6363
closeOnNavigation?: boolean;
64+
componentFactoryResolver?: ComponentFactoryResolver;
6465
data?: D | null;
6566
direction?: Direction;
6667
disableClose?: boolean;

0 commit comments

Comments
 (0)