Skip to content

Commit d2af80b

Browse files
crisbetojelbourn
authored andcommitted
fix(portal): use portal's ComponentFactoryResolver in portal outlet directive (#13886)
Follow-up from #12677 where we missed to add the new functionality to the `CdkPortalOutlet` directive. Fixes #9712.
1 parent 7c202ec commit d2af80b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/cdk/portal/portal-directives.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
9292
this._attachedPortal = portal;
9393
}
9494

95+
/** Emits when a portal is attached to the outlet. */
9596
@Output() attached: EventEmitter<CdkPortalOutletAttachedRef> =
9697
new EventEmitter<CdkPortalOutletAttachedRef>();
9798

@@ -125,8 +126,8 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
125126
portal.viewContainerRef :
126127
this._viewContainerRef;
127128

128-
const componentFactory =
129-
this._componentFactoryResolver.resolveComponentFactory(portal.component);
129+
const resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
130+
const componentFactory = resolver.resolveComponentFactory(portal.component);
130131
const ref = viewContainerRef.createComponent(
131132
componentFactory, viewContainerRef.length,
132133
portal.injector || viewContainerRef.injector);

src/cdk/portal/portal.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ describe('Portals', () => {
2929

3030
describe('CdkPortalOutlet', () => {
3131
let fixture: ComponentFixture<PortalTestApp>;
32+
let componentFactoryResolver: ComponentFactoryResolver;
3233

3334
beforeEach(() => {
3435
fixture = TestBed.createComponent(PortalTestApp);
36+
37+
inject([ComponentFactoryResolver], (cfr: ComponentFactoryResolver) => {
38+
componentFactoryResolver = cfr;
39+
})();
3540
});
3641

3742
it('should load a component into the portal', () => {
@@ -311,6 +316,20 @@ describe('Portals', () => {
311316
expect(instance.portalOutlet.hasAttached()).toBe(true);
312317
});
313318

319+
it('should use the `ComponentFactoryResolver` from the portal, if available', () => {
320+
const spy = jasmine.createSpy('resolveComponentFactorySpy');
321+
const portal = new ComponentPortal(PizzaMsg, undefined, undefined, {
322+
resolveComponentFactory: (...args: any[]) => {
323+
spy();
324+
return componentFactoryResolver.resolveComponentFactory
325+
.apply(componentFactoryResolver, args);
326+
}
327+
});
328+
329+
fixture.componentInstance.portalOutlet.attachComponentPortal(portal);
330+
expect(spy).toHaveBeenCalled();
331+
});
332+
314333
});
315334

316335
describe('DomPortalOutlet', () => {
@@ -324,8 +343,8 @@ describe('Portals', () => {
324343
let appRef: ApplicationRef;
325344
let deps = [ComponentFactoryResolver, Injector, ApplicationRef];
326345

327-
beforeEach(inject(deps, (dcl: ComponentFactoryResolver, i: Injector, ar: ApplicationRef) => {
328-
componentFactoryResolver = dcl;
346+
beforeEach(inject(deps, (cfr: ComponentFactoryResolver, i: Injector, ar: ApplicationRef) => {
347+
componentFactoryResolver = cfr;
329348
injector = i;
330349
appRef = ar;
331350
}));

0 commit comments

Comments
 (0)