Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Add support for setting up component factory resolvers in modal servi…
Browse files Browse the repository at this point in the history
…ce (#471)
  • Loading branch information
Hinton committed Aug 31, 2021
1 parent 30419a6 commit d505318
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions angular/src/components/modal/dynamic-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
AfterViewInit,
ChangeDetectorRef,
Component,
ComponentFactoryResolver,
ComponentRef,
ElementRef,
OnDestroy,
Expand All @@ -11,6 +10,8 @@ import {
ViewContainerRef
} from '@angular/core';

import { ModalService } from '../../services/modal.service';

import { ModalRef } from './modal.ref';

@Component({
Expand All @@ -25,7 +26,7 @@ export class DynamicModalComponent implements AfterViewInit, OnDestroy {
childComponentType: Type<any>;
setComponentParameters: (component: any) => void;

constructor(private componentFactoryResolver: ComponentFactoryResolver, private cd: ChangeDetectorRef,
constructor(private modalService: ModalService, private cd: ChangeDetectorRef,
private el: ElementRef<HTMLElement>, public modalRef: ModalRef) {}

ngAfterViewInit() {
Expand All @@ -39,7 +40,7 @@ export class DynamicModalComponent implements AfterViewInit, OnDestroy {
}

loadChildComponent(componentType: Type<any>) {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType);
const componentFactory = this.modalService.resolveComponentFactory(componentType);

this.modalContentRef.clear();
this.componentRef = this.modalContentRef.createComponent(componentFactory);
Expand Down
17 changes: 17 additions & 0 deletions angular/src/services/modal.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ApplicationRef,
ComponentFactory,
ComponentFactoryResolver,
ComponentRef,
EmbeddedViewRef,
Expand All @@ -23,6 +24,10 @@ export class ModalConfig<D = any> {
export class ModalService {
protected modalCount = 0;

// Lazy loaded modules are not available in componentFactoryResolver,
// therefore modules needs to manually initialize their resolvers.
private factoryResolvers: Map<Type<any>, ComponentFactoryResolver> = new Map();

constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef,
private injector: Injector) {}

Expand Down Expand Up @@ -51,6 +56,18 @@ export class ModalService {
return modalRef;
}

registerComponentFactoryResolver<T>(componentType: Type<T>, componentFactoryResolver: ComponentFactoryResolver): void {
this.factoryResolvers.set(componentType, componentFactoryResolver);
}

resolveComponentFactory<T>(componentType: Type<T>): ComponentFactory<T> {
if (this.factoryResolvers.has(componentType)) {
return this.factoryResolvers.get(componentType).resolveComponentFactory(componentType);
}

return this.componentFactoryResolver.resolveComponentFactory(componentType);
}

protected openInternal(componentType: Type<any>, config?: ModalConfig, attachToDom?: boolean):
[ModalRef, ComponentRef<DynamicModalComponent>] {

Expand Down

0 comments on commit d505318

Please sign in to comment.