From d50531886bb8a797e98a3aeea08a7ed54e768788 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Tue, 31 Aug 2021 16:35:20 +0200 Subject: [PATCH] Add support for setting up component factory resolvers in modal service (#471) --- .../components/modal/dynamic-modal.component.ts | 7 ++++--- angular/src/services/modal.service.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/angular/src/components/modal/dynamic-modal.component.ts b/angular/src/components/modal/dynamic-modal.component.ts index d6deb99de..d3816520d 100644 --- a/angular/src/components/modal/dynamic-modal.component.ts +++ b/angular/src/components/modal/dynamic-modal.component.ts @@ -2,7 +2,6 @@ import { AfterViewInit, ChangeDetectorRef, Component, - ComponentFactoryResolver, ComponentRef, ElementRef, OnDestroy, @@ -11,6 +10,8 @@ import { ViewContainerRef } from '@angular/core'; +import { ModalService } from '../../services/modal.service'; + import { ModalRef } from './modal.ref'; @Component({ @@ -25,7 +26,7 @@ export class DynamicModalComponent implements AfterViewInit, OnDestroy { childComponentType: Type; setComponentParameters: (component: any) => void; - constructor(private componentFactoryResolver: ComponentFactoryResolver, private cd: ChangeDetectorRef, + constructor(private modalService: ModalService, private cd: ChangeDetectorRef, private el: ElementRef, public modalRef: ModalRef) {} ngAfterViewInit() { @@ -39,7 +40,7 @@ export class DynamicModalComponent implements AfterViewInit, OnDestroy { } loadChildComponent(componentType: Type) { - const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType); + const componentFactory = this.modalService.resolveComponentFactory(componentType); this.modalContentRef.clear(); this.componentRef = this.modalContentRef.createComponent(componentFactory); diff --git a/angular/src/services/modal.service.ts b/angular/src/services/modal.service.ts index f5573b401..9f85a8102 100644 --- a/angular/src/services/modal.service.ts +++ b/angular/src/services/modal.service.ts @@ -1,5 +1,6 @@ import { ApplicationRef, + ComponentFactory, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, @@ -23,6 +24,10 @@ export class ModalConfig { 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, ComponentFactoryResolver> = new Map(); + constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef, private injector: Injector) {} @@ -51,6 +56,18 @@ export class ModalService { return modalRef; } + registerComponentFactoryResolver(componentType: Type, componentFactoryResolver: ComponentFactoryResolver): void { + this.factoryResolvers.set(componentType, componentFactoryResolver); + } + + resolveComponentFactory(componentType: Type): ComponentFactory { + if (this.factoryResolvers.has(componentType)) { + return this.factoryResolvers.get(componentType).resolveComponentFactory(componentType); + } + + return this.componentFactoryResolver.resolveComponentFactory(componentType); + } + protected openInternal(componentType: Type, config?: ModalConfig, attachToDom?: boolean): [ModalRef, ComponentRef] {