Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(portal): allow for custom ComponentFactoryResolver to be associated with portal #12677

Merged
merged 1 commit into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cdk/portal/dom-portal-outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export class DomPortalOutlet extends BasePortalOutlet {
* @returns Reference to the created component.
*/
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
let componentFactory = this._componentFactoryResolver.resolveComponentFactory(portal.component);
const resolver = portal.componentFactoryResolver || this._componentFactoryResolver;
const componentFactory = resolver.resolveComponentFactory(portal.component);
let componentRef: ComponentRef<T>;

// If the portal specifies a ViewContainerRef, we will use that as the attachment point
Expand Down
17 changes: 16 additions & 1 deletion src/cdk/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ describe('Portals', () => {
let host: DomPortalOutlet;
let injector: Injector;
let appRef: ApplicationRef;

let deps = [ComponentFactoryResolver, Injector, ApplicationRef];

beforeEach(inject(deps, (dcl: ComponentFactoryResolver, i: Injector, ar: ApplicationRef) => {
componentFactoryResolver = dcl;
injector = i;
Expand Down Expand Up @@ -468,6 +468,21 @@ describe('Portals', () => {

expect(spy).toHaveBeenCalled();
});

it('should use the `ComponentFactoryResolver` from the portal, if available', () => {
const spy = jasmine.createSpy('resolveComponentFactorySpy');
const portal = new ComponentPortal(PizzaMsg, undefined, undefined, {
resolveComponentFactory: (...args: any[]) => {
spy();
return componentFactoryResolver.resolveComponentFactory
.apply(componentFactoryResolver, args);
}
});

host.attachComponentPortal(portal);
expect(spy).toHaveBeenCalled();
});

});
});

Expand Down
13 changes: 11 additions & 2 deletions src/cdk/portal/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
ElementRef,
ComponentRef,
EmbeddedViewRef,
Injector
Injector,
ComponentFactoryResolver,
} from '@angular/core';
import {
throwNullPortalOutletError,
Expand Down Expand Up @@ -93,14 +94,22 @@ export class ComponentPortal<T> extends Portal<ComponentRef<T>> {
/** [Optional] Injector used for the instantiation of the component. */
injector?: Injector | null;

/**
* Alternate `ComponentFactoryResolver` to use when resolving the associated component.
* Defaults to using the resolver from the outlet that the portal is attached to.
*/
componentFactoryResolver?: ComponentFactoryResolver | null;

constructor(
component: ComponentType<T>,
viewContainerRef?: ViewContainerRef | null,
injector?: Injector | null) {
injector?: Injector | null,
componentFactoryResolver?: ComponentFactoryResolver | null) {
super();
this.component = component;
this.viewContainerRef = viewContainerRef;
this.injector = injector;
this.componentFactoryResolver = componentFactoryResolver;
}
}

Expand Down