Initialize component only once when attaching to a portal

Viewed 331

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.

1 Answers
Related