shlomiassaf / ngx-modialog

Modal / Dialog for Angular
http://shlomiassaf.github.io/ngx-modialog
MIT License
686 stars 242 forks source link

Provide component by name for a custom modal #257

Closed rhegner closed 7 years ago

rhegner commented 7 years ago

Let's say I have something like this to open a custom modal:

this.modal.open(MyComponent, overlayConfigFactory(new MyContext(7), BSModalContext));

In my real application which component should be displayed in the modal is decided at runtime and I need the ability to open a modal with a component name (which means I have the name of the component as a string).

I tried things like these but none of these work:

this.modal.open(this["MyComponent"], overlayConfigFactory(new MyContext(7), BSModalContext));

this.modal.open(eval("MyComponent"), overlayConfigFactory(new MyContext(7), BSModalContext));

How can this be done?

shlomiassaf commented 7 years ago

This is not related to angular2-modal.

Never do eval("MyComponent"), aside from it having no logic, eval is not a smart move.

You need to get a component, you can use a string. If you need to get a component dynamically use a function and return it.

There is no registry mechanizem in angular that can retrieve a component by name. The name is not how a component is identified.