question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot create component without inserting into DOM without the deprecated `ComponentFactory`

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Since Angular 13 ComponentFactory.create() is deprecated. It is advertised that we should instead use ViewContainerRef.createComponent(). However the behavior between those two methods are different and cannot necessarily replace each other for all use-cases.

My use-case is to create a component dynamically, not insert it into DOM, call a few custom methods on it, and then destroy the component. But ViewContainerRef.createComponent() will always insert the newly created component into DOM, wreaking havoc in my layout.

Basically I’d like to do something like that:

private validate(component: Type<DropdownComponent>): boolean {
    const injector = Injector.create({providers: this.getMyCustomProvider(), parent: this.getMyParentInjector()});
    const factory = this.componentFactoryResolver.resolveComponentFactory<DropdownComponent>(component);

    const dropdownComponentRef = factory.create(injector);
    const result = dropdownComponentRef.instance.isValid();
    dropdownComponentRef.destroy();

    return result;
}

Was it intentional that a component cannot be instantiated without being inserted into DOM anymore ? Or should it be considered a regression in the feature set of the framework ?

Or is there an alternative API that would allow me to instantiate and destroy components without ever inserting them into DOM ?

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 13.2.5
Node: 14.19.0
Package Manager: yarn 1.22.17
OS: linux x64

Angular: 13.2.5
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, language-service, localize, material
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1302.5
@angular-devkit/build-angular   13.2.5
@angular-devkit/core            13.2.5
@angular-devkit/schematics      13.2.5
@angular/flex-layout            13.0.0-beta.38
@schematics/angular             13.2.5
ng-packagr                      13.2.1
rxjs                            7.5.4
typescript                      4.5.5

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:30
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

22reactions
AndrewKushnircommented, Mar 30, 2022

Hi, just a quick update: we plan to look into replacement APIs for the described use-case. The new API should provide an ability to create a ComponentRef instance, i.e. you should be able to do something like this:

const appRef = injector.get(ApplicationRef);
const compRef = createComponentRef(MyComponent, injector);
appRef.attachView(compRef.hostView);

We don’t have an ETA yet, but we’d make sure that it’s available before we remove ComponentFactory from the public API.

5reactions
pkozlowski-opensourcecommented, Jul 8, 2022

@tonysamperi the alternative is being worked on in #46685

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create dynamic components using service without ...
How to create dynamic components using service without ComponentFactoryResolver as it is deprecated in angular.
Read more >
Its possible to create a dynamic component without ...
I want create a Component without attaching it to the DOM. ... to ComponentFactory.create , which shipped I believe in 14.1 just recently....
Read more >
ViewContainerRef
Deprecated Angular no longer requires component factories to dynamically create components. Use different signature of the createComponent method, ...
Read more >
ReactDOM
It may be possible to insert a component to an existing DOM node without overwriting the existing children. render() currently returns a reference...
Read more >
API Reference
You can pass an arbitrary classname to a styled component without problem ... Learn more about this constructor in the Attaching Additional Props...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found