Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Angular ComponentFactoryResolver + ComponentFactory in a service?

Tags:

angular

I'm looking for a substitute for deprecated ComponentFactoryResolver and ComponentFactory classes in Angular 13. The official documentation as well as another question here on SO suggest using ViewContainerRef, but my application instantiates Angular components in a service, outside of the DOM. There is no ViewContainerRef available.

The instantiated components are not to be shown to the user. They are used to generate in-memory SVGs that are then rasterized and used for further purposes.

This is what I've used until now in my service:

let factoryHistogram = this.resolver.resolveComponentFactory(HistogramComponent);
let newNode = document.createElement('div');
this.histogramComponent = factoryHistogram.create(this.injector, [], newNode);
this.app.attachView(this.histogramComponent.hostView);

How can I achieve the same in Angular 13?

like image 898
LubosD Avatar asked Oct 11 '25 18:10

LubosD


1 Answers

The fact that it is no longer possible to create a component without also inserting it into DOM has been reported as a possible regression over there:

https://github.com/angular/angular/issues/45263

Hopefully the core team will be able to figure out a way to keep supporting this use case.