0

This question has been asked before and received differing answers, so I thought I'd ask again and be a little more specific. I've been going over a lot of articles from Max Koretskyi as listed in this SO question about Embedded/Host views, and it seems like it should be possible to create 2+ ViewContainerRefs and insert the same ComponentRef<MyComponent> instance into both of them.

This question is solved by dynamically storing a component factory in a service and detaching/attaching that component instance to a ViewContainerRef every time a route change is made. I forked the stackblitz from that question and tried to create another one in which I attempt to load 2 views from the same instance on the same route, but it appears as if when you call viewContainerRef.insert on a componentRef.hostView, it looks like it automatically detaches from whatever viewRef it's currently attached to.

Is this the case, and if so, does that mean it's impossible to accomplish? Otherwise, is there another way to do this? (Short of the obvious method of creating 2 components and storing the container state elsewhere?)

Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24
mcheah
  • 1,209
  • 11
  • 28
  • 1
    Just wondering, why? As you know reusing the same component is easy. If, within that component there are static properties, then each user can access the same things. However a service is probably a better idea. Html itself, as far as I know has no ability to tap into the rendered element (the view) – JWP May 22 '20 at 23:31
  • This was one of those situations where I went down a rabbit hole and almost certainly wasted more time trying to do something in a more simple manner instead of just doing it the way I knew it would work. I have a fairly large route with lots of components that are removed from the DOM (ie. inside dialogs and tabs) but I need a way to render everything when a user prints the page. If I just create a new component instance I'd need to reproduce all changes that a user made to each component, so I was hoping I could share the same instance and move the component around. (again, rabbit hole.) – mcheah May 24 '20 at 00:30

0 Answers0