简体   繁体   中英

Pass Angular2 Component as parameter

I need to create a general solution to open a modal from whatever place inside the application by passing some parameters, simply these parameters might be size (small, large, full-screen), dismissable (true or false) and the most important parameter is the component that I need to display inside this modal. My question is how can I pass a full component to a method inside service.

You can simply pass component class to your service:

service.openModal(component = AComponent, ...rest) {}

If you want to pass it as a string, you can declare what components will be used in your service and look for them by name. Something like this:

service.dynamicComponents = [AComponent, BComponent];

service.openModal(params = { component: 'AComponent' }) {
  const component = this.dynamicComponents
                        .find(o => o.constructor.name === params.component)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM