15

I'm trying to create modal service with ability to show arbitrary modal. Currently, to create dynamic component, I'm storing placeholder for it in view:

 <div #container></div>
 ...
 @ViewChild("dialogContainer", {read: ViewContainerRef})
 dialogContainer:ViewContainerRef;

And than creating component:

let factory = this.componentResolver.resolveComponentFactory(Dialog);
this.componentReference = this.dialogContainer.createComponent(factory);

Is there way to move all this logic out of component to service, with ability to create/remove view container in body, or any other DOM element?

lexigren
  • 530
  • 4
  • 16
  • i need genereate container like this ````
    ``` and get respective id viewcontainer reference
    – Jagadeesh Govindaraj Nov 14 '16 at 09:39
  • i need load mutiple dynamic components in mutiple dynammic containers – Jagadeesh Govindaraj Nov 14 '16 at 09:40
  • Check: http://stackoverflow.com/questions/36566698/cant-initialize-dynamically-appended-html-component-in-angular-2 – eko Nov 16 '16 at 05:42
  • What do you mean by *modal service*? In the code you provided the template variable does not match the `ViewChild` parameter. I also do not see how the current title relates to the issue described in the question. – Johannes Dec 28 '20 at 13:33

1 Answers1

2

It dosen't create ViewContainerRef but It helps you create dynamically a popup window as separate Component.

I created plunker example for you, and I didn't embed it here because stackowerflow code snippet dosen't support all features which I need in order to create example :) http://embed.plnkr.co/XFQAAHDAyrRAih3RTvHH/

Kld
  • 6,970
  • 3
  • 37
  • 50
igorzg
  • 1,506
  • 14
  • 17