Skip to content

Commit

Permalink
feat(dialog): allow for custom ComponentFactoryResolver to be passed …
Browse files Browse the repository at this point in the history
…in (angular#16437)

Allows for a different `ComponentFactoryResolver` to be passed in when creating a dialog.

Fixes angular#16431.
  • Loading branch information
crisbeto authored and jelbourn committed Jul 19, 2019
1 parent a415b52 commit 565bd7d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/material/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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

/** Alternate `ComponentFactoryResolver` to use when resolving the associated component. */
componentFactoryResolver?: ComponentFactoryResolver;

// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
}
16 changes: 15 additions & 1 deletion src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/material/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MatDialogContainer>(containerPortal);

return containerRef.instance;
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export declare class MatDialogConfig<D = any> {
autoFocus?: boolean;
backdropClass?: string;
closeOnNavigation?: boolean;
componentFactoryResolver?: ComponentFactoryResolver;
data?: D | null;
direction?: Direction;
disableClose?: boolean;
Expand Down

0 comments on commit 565bd7d

Please sign in to comment.