What is entryComponents in angular ngModule?
You won't get explanation better than Angular docs: entry-components and ngmodule-faq.
And below is the explanation from the angular docs.
An entry component is any component that Angular loads imperatively by type.
A component loaded declaratively via its selector is not an entry component.
Most application components are loaded declaratively. Angular uses the component's selector to locate the element in the template. It then creates the HTML representation of the component and inserts it into the DOM at the selected element. These aren't entry components.
A few components are only loaded dynamically and are never referenced in a component template.
The bootstrapped root
AppComponent
is an entry component. True, its selector matches an element tag in index.html. Butindex.html
isn't a component template and theAppComponent
selector doesn't match an element in any component template.Angular loads AppComponent dynamically because it's either listed by type in
@NgModule.bootstrap
or boostrapped imperatively with the module's ngDoBootstrap method.Components in route definitions are also entry components. A route definition refers to a component by its type. The router ignores a routed component's selector (if it even has one) and loads the component dynamically into a
RouterOutlet
.The compiler can't discover these entry components by looking for them in other component templates. You must tell it about them by adding them to the
entryComponents
list.Angular automatically adds the following types of components to the module's
entryComponents
:
- The component in the
@NgModule.bootstrap
list.- Components referenced in router configuration.
You don't have to mention these components explicitly, although doing so is harmless.
The other answers mention this but the basic summary is:
- It's needed when a Component is NOT used in the html , ex:
<my-component />
- For example with Angular Material Dialogs you use them indirectly, they are created inside the TS code and not the html:
openDialog() {
const dialogRef = this.dialog.open(MyExampleDialog, { width: '250px'});
}
This requires you to register it as an entryComponent:
entryComponents: [MyExampleDialog]
Otherwise you get a error:
ERROR Error: No component factory found for MyExampleDialog. Did you add it to @NgModule.entryComponents?
This is for dynamically added components that are added using ViewContainerRef.createComponent()
. Adding them to entryComponents
tells the offline template compiler to compile them and create factories for them.
The components registered in route configurations are added automatically to entryComponents
as well because router-outlet
also uses ViewContainerRef.createComponent()
to add routed components to the DOM.
Offline template compiler (OTC) only builds components that are actually used. If components aren't used in templates directly the OTC can't know whether they need to be compiled. With entryComponents you can tell the OTC to also compile this components so they are available at runtime.
What is an entry component? (angular.io)
NgModule docs (angular.io)
Defines the components that should be compiled as well when this component is defined. For each components listed here, Angular will create a ComponentFactory and store it in the ComponentFactoryResolver.
If you don't list a dynamically added component to entryComponents
you'll get an error message a bout a missing factory because Angular won't have created one.
See also https://angular.io/docs/ts/latest/cookbook/dynamic-component-loader.html