I am trying to make a reusable quickView modal in my app to load any component dynamically using the ng-bootstrap modal library
It's working fine as far I am loading the same example component as shown in docs, but not for the components I created to test.
How can I use the quickView modal for dynamic components creation to load in modal-body?
https://stackblitz.com/edit/angular-quickview
I am using simple if/else to open a component in the modal based on name string.
<button class="btn btn-lg btn-outline-secondary mr-2" (click)="open('default')">Launch default</button>
<button class="btn btn-lg btn-outline-danger mr-2" (click)="open('red')">Launch red</button>
<button class="btn btn-lg btn-outline-primary" (click)="open('blue')">Launch blue</button>
open(name: string) {
if (name === "default") {
const modalRef = this.modalService.open(NgbdModalContent);
modalRef.componentInstance.name = "Default";
} else if (name === "red") {
const modalRef = this.modalService.open(RedComponent);
modalRef.componentInstance.name = "Red";
} else if (name === "blue") {
const modalRef = this.modalService.open(BlueComponent);
modalRef.componentInstance.name = "Blue";
}
}
I also tried with componentFactoryResolver to inject my component into modal-body but that also throw error Error: Cannot read property 'viewContainerRef' of undefined
<div class="modal-body">
<ng-template quickView></ng-template>
</div>
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
RedComponent
);
const viewContainerRef = this.quickView.viewContainerRef;
viewContainerRef.clear();
const componentRef = viewContainerRef.createComponent<any>(
componentFactory
);
componentRef.instance.name = "Red";
