1

I need to pass a component name as argument to a function and receive it and use it. But only string is received. Below is the code

(click)="this.myapp.openModal('AddContactModelComponent','xl')"

I am receiving it here to use as component

 openModal(component:any,size:string) {
    // const modalRef = this.modalService.open(ModalComponent);
    const modalRef = this.modalService.open(
      component,
      {
       size: size, 
      });
      this.modal_title ='test';
  }

1 Answer 1

1

You are passing in a string as the component argument ('AddContactModelComponent' in your example).

You will most likely need a mapper that openModal can use like so

openModal(componentName: string, size: string) {
  const mapper = {
    'AddContactModelComponent': AddContactModelComponent,
    'AnotherString': AnotherComponent,
  };

  const modalRef = this.modalService.open(
    mapper[componentName],
    {
      size: size,
    });
}

Then you can call it as desired in your template

(click)="this.myapp.openModal('AddContactModelComponent','xl')"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.