How to dynamically create and manage multiple popup dialogs in Angular?

I am working on an Angular(17.x) project where I need to create and manage multiple popup dialogs dynamically. I aim to avoid manually creating separate components, inputs, and outputs for each popup. Instead, I want to use a service to create these dialogs and handle their events dynamically.

I have tried to implement a solution using ViewContainerRef and a custom service to open dialogs. However, the dialogs are not opening as expected, and I am encountering issues with binding inputs and outputs dynamically. Below is my current implementation.

Html file

<button (click)="popupAOpen()">Open Popup A</button>
<button (click)="popupBOpen()">Open Popup B</button>
<button (click)="popupCOpen()">Open Popup C</button>
<ng-template #dialogContainer></ng-template>

component typescript file

import { Component, ViewChild, ViewContainerRef } from '@angular/core';
import { PopupWindowService } from './popup-window.service';

@Component({
  selector: 'app-your-component',
  templateUrl: './your-component.component.html',
  styleUrls: ['./your-component.component.css']
})
export class YourComponent {
  @ViewChild('dialogContainer', { read: ViewContainerRef }) viewContainerRef!: ViewContainerRef;
  
  inputA1: any = 'exampleA1';
  inputA2: any = 'exampleA2';
  inputB1: any = 'exampleB1';
  inputB2: any = 'exampleB2';
  inputC1: any = 'exampleC1';
  inputC2: any = 'exampleC2';

  constructor(private dialogService: PopupWindowService) {}

  popupAOpen() {
    this.dialogService.openDialog(
      this.viewContainerRef,
      PopUpA,
      { isVisible: true, inputA1: this.inputA1, inputA2: this.inputA2 },
      { outputA1: this.outputA1.bind(this), outputA2: this.outputA2.bind(this) }
    );
  }

  popupBOpen() {
    this.dialogService.openDialog(
      this.viewContainerRef,
      PopUpB,
      { isVisible: true, inputB1: this.inputB1, inputB2: this.inputB2 },
      { outputB1: this.outputB1.bind(this), outputB2: this.outputB2.bind(this) }
    );
  }

  popupCOpen() {
    this.dialogService.openDialog(
      this.viewContainerRef,
      PopUpC,
      { isVisible: true, inputC1: this.inputC1, inputC2: this.inputC2 },
      { outputC1: this.outputC1.bind(this), outputC2: this.outputC2.bind(this) }
    );
  }

  outputA1() { /* Handle outputA1 */ }
  outputA2() { /* Handle outputA2 */ }
  outputB1() { /* Handle outputB1 */ }
  outputB2() { /* Handle outputB2 */ }
  outputC1() { /* Handle outputC1 */ }
  outputC2() { /* Handle outputC2 */ }
}

PopupWindowService

import { Injectable, ComponentRef, ApplicationRef, Injector, ViewContainerRef } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class PopupWindowService {
  private dialogRefs: ComponentRef<any>[] = [];

  constructor(
    private appRef: ApplicationRef,
    private injector: Injector
  ) {}

  openDialog(viewContainerRef: ViewContainerRef, component: any, inputs: any, outputs: any = {}): ComponentRef<any> {
    const contentRef = viewContainerRef.createComponent(component, { injector: this.injector });
    
    Object.keys(inputs).forEach(inputName => {
      contentRef.instance[inputName] = inputs[inputName];
    });

    const dialogRef = viewContainerRef.createComponent(DialogComponent, { injector: this.injector });
    dialogRef.instance.content = contentRef.location.nativeElement;
    this.appRef.attachView(dialogRef.hostView);
    this.appRef.attachView(contentRef.hostView);

    document.body.appendChild((dialogRef.hostView as any).rootNodes[0] as HTMLElement);
    this.dialogRefs.push(dialogRef);

    Object.keys(outputs).forEach(outputName => {
      if (contentRef.instance[outputName]) {
        contentRef.instance[outputName].subscribe(outputs[outputName]);
      }
    });

    dialogRef.instance.show();
    contentRef.instance.closeDialog.subscribe(() => this.closeDialog(dialogRef, contentRef));

    return dialogRef;
  }

  closeDialog(dialogRef: ComponentRef<any>, contentRef: ComponentRef<any>) {
    dialogRef.instance.hide();
    this.appRef.detachView(dialogRef.hostView);
    this.appRef.detachView(contentRef.hostView);
    dialogRef.destroy();
    contentRef.destroy();
    this.dialogRefs = this.dialogRefs.filter(ref => ref !== dialogRef);
  }
}

I have attempted to dynamically create and manage popups using a service, but the dialogs are not opening correctly. I suspect there might be an issue with how I am binding inputs and outputs dynamically.

Specific Questions

  1. How can I correctly bind inputs and outputs dynamically to the popup
    components?
  2. Are there any improvements or alternatives to dynamically manage multiple popups in Angular?

Any help or insights would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật