Getting to Know the createComponent API in Angular
Angular v14.1 added a new function named createComponent
. This function can cover the use-cases where the ComponentFactory
symbol was previously used.
The function allows creating a ComponentRef
instance based on provided Component
and a set of options:
It’s possible to pass a new environment injector inherited from the current one:
It’s possible to pass an element injector:
The component can be appended to a host
element:
We can pass projectableNodes
— A list of DOM nodes that should be projected through <ng-content>
of the new component instance:
The createComponent
API can be used for two main purposes:
And dynamically create components outside components and directives.
Follow me on Medium or Twitter to read more about Angular and JS!