I need to attach and detach an instance of a set of components from a portal dynamically without reinitializing it every time I attach as it degrades the performance of the application a lot.
portal = new ComponentPortal(MyComponent);
this.portalHost = new DomPortalHost(
this.elementRef.nativeElement,
this.componentFactoryResolver,
this.appRef,
this.injector
);
const componentRef = this.portalHost.attach(this.portal);
componentRef.instance.myInput = data;
componentRef.instance.myOutput.subscribe(...);
componentRef.changeDetectorRef.detectChanges();
This is the way that is explained in this question. But every time the component is reattached it will reinitialize.
RouteReuseStrategy
as explained in this answer: https://mcmap.net/q/1918843/-how-to-keep-the-latest-state-of-angular-components – Drift