NgModules help organize an application into cohesive blocks of functionality.
The NgModules page guides you
from the most elementary
@NgModule to a multi-faceted sample with lazy-loaded modules.
This page answers the questions many developers ask about NgModule design and implementation.
These FAQs assume that you have read the NgModules page.
- What classes should I add to declarations?
- What is a declarable?
- What classes should I not add to declarations?
- Why list the same component in multiple NgModule properties?
- What does "Can't bind to 'x' since it isn't a known property of 'y'" mean?
- What should I import?
- Should I import BrowserModule or CommonModule?
- What if I import the same module twice?
- What should I export?
- What should I not export?
- Can I re-export imported classes and modules?
- What is the forRoot method?
- Why is a service provided in a feature module visible everywhere?
- Why is a service provided in a lazy-loaded module visible only to that module?
- What if two modules provide the same service?
- How do I restrict service scope to a module?
- Should I add app-wide providers to the root AppModule or the root AppComponent?
- Should I add other providers to a module or a component?
- Why is it bad if SharedModule provides a service to a lazy-loaded module?
- Why does lazy loading create a child injector?
- How can I tell if a module or service was previously loaded?
- What is an entry component?
- What is the difference between a bootstrap component and an entry component?
- When do I add components to entryComponents?
- Why does Angular need entryComponents?
- What kinds of modules should I have and how should I use them?
- How does Angular find components, directives, and pipes in a template?
- What is a "template reference"?
- What is the Angular compiler?
- Can you summarize the NgModule API?
What classes should I add to declarations?
Add declarable classes—components, directives, and pipes—to a
Declare these classes in exactly one module of the application. Declare them in this module if they belong to this module.
What is a declarable?
Declarables are the class types—components, directives, and pipes—that
you can add to a module's
They're the only classes that you can add to
What classes should I not add to declarations?
Add only declarable classes to a module's
Do not declare the following:
- A class that's already declared in another module, whether an app module, @NgModule, or third-party module.
- An array of directives imported from another module.
For example, don't declare FORMS_DIRECTIVES from
- Module classes.
- Service classes.
- Non-Angular classes and objects, such as strings, numbers, functions, entity models, configurations, business logic, and helper classes.
Why list the same component in multiple NgModule properties?
AppComponent is often listed in both
You might see
HeroComponent listed in
While that seems redundant, these properties have different functions. Membership in one list doesn't imply membership in another list.
AppComponentcould be declared in this module but not bootstrapped.
AppComponentcould be bootstrapped in this module but declared in a different feature module.
HeroComponentcould be imported from another app module (so you can't declare it) and re-exported by this module.
HeroComponentcould be exported for inclusion in an external component's template as well as dynamically loaded in a pop-up dialog.
What does "Can't bind to 'x' since it isn't a known property of 'y'" mean?
This error usually means that you haven't declared the directive "x" or haven't imported the module to which "x" belongs.
For example, if "x" is
ngModel, you probably haven't imported the
Perhaps you declared "x" in an application sub-module but forgot to export it?
The "x" class isn't visible to other modules until you add it to the
What should I import?
Import modules whose public (exported) declarable classes you need to reference in this module's component templates.
This always means importing
@angular/common for access to
the Angular directives such as
You can import it directly or from another module that re-exports it.
if your components have
[(ngModel)] two-way binding expressions.
Import shared and feature modules when this module's components incorporate their components, directives, and pipes.
Import only BrowserModule in the root
Should I import BrowserModule or CommonModule?
The root application module (
AppModule) of almost every browser application
BrowserModule provides services that are essential to launch and run a browser app.
BrowserModule also re-exports
which means that components in the
AppModule module also have access to
the Angular directives every app needs, such as
Do not import
BrowserModule in any other module.
Feature modules and lazy-loaded modules should import
They need the common directives. They don't need to re-install the app-wide providers.
BrowserModule throws an error if you try to lazy load a module that imports it.
CommonModule also frees feature modules for use on any target platform, not just browsers.
What if I import the same module twice?
That's not a problem. When three modules all import Module 'A', Angular evaluates Module 'A' once, the first time it encounters it, and doesn't do so again.
That's true at whatever level
A appears in a hierarchy of imported modules.
When Module 'B' imports Module 'A', Module 'C' imports 'B', and Module 'D' imports
[C, B, A],
then 'D' triggers the evaluation of 'C', which triggers the evaluation of 'B', which evaluates 'A'.
When Angular gets to the 'B' and 'A' in 'D', they're already cached and ready to go.
Angular doesn't like modules with circular references, so don't let Module 'A' import Module 'B', which imports Module 'A'.
What should I export?
Export declarable classes that components in other modules are able to reference in their templates. These are your public classes. If you don't export a class, it stays private, visible only to other component declared in this module.
You can export any declarable class—components, directives, and pipes—whether it's declared in this module or in an imported module.
You can re-export entire imported modules, which effectively re-exports all of their exported classes. A module can even export a module that it doesn't import.
What should I not export?
Don't export the following:
- Private components, directives, and pipes that you need only within components declared in this module. If you don't want another module to see it, don't export it.
- Non-declarable objects such as services, functions, configurations, and entity models.
- Components that are only loaded dynamically by the router or by bootstrapping. Such entry components can never be selected in another component's template. While there's no harm in exporting them, there's also no benefit.
- Pure service modules that don't have public (exported) declarations.
For example, there's no point in re-exporting
HttpModulebecause it doesn't export anything. It's only purpose is to add http service providers to the application as a whole.
Can I re-export classes and modules?
Modules are a great way to selectively aggregate classes from other modules and re-export them in a consolidated, convenience module.
A module can re-export entire modules, which effectively re-exports all of their exported classes.
BrowserModule exports a couple of modules like this:
A module can export a combination of its own declarations, selected imported classes, and imported modules.
Don't bother re-exporting pure service modules.
Pure service modules don't export declarable classes that another module could use.
For example, there's no point in re-exporting
HttpModule because it doesn't export anything.
It's only purpose is to add http service providers to the application as a whole.
What is the forRoot method?
forRoot static method is a convention that makes it easy for developers to configure the module's providers.
RouterModule.forRoot method is a good example.
Apps pass a
Routes object to
RouterModule.forRoot in order to configure the app-wide
Router service with routes.
RouterModule.forRoot returns a ModuleWithProviders.
You add that result to the
imports list of the root
Only call and import a
.forRoot result in the root application module,
Importing it in any other module, particularly in a lazy-loaded module,
is contrary to the intent and will likely produce a runtime error.
RouterModule also offers a
forChild static method for configuring the routes of lazy-loaded modules.
forRoot and forChild are conventional names for methods that configure services in root and feature modules respectively.
Angular doesn't recognize these names but Angular developers do. Follow this convention when you write similar modules with configurable service providers.
Why is a service provided in a feature module visible everywhere?
Providers listed in the
@NgModule.providers of a bootstrapped module have application scope.
Adding a service provider to
@NgModule.providers effectively publishes the service to the entire application.
When you import a module,
Angular adds the module's service providers (the contents of its
to the application root injector.
This makes the provider visible to every class in the application that knows the provider's lookup token.
This is by design.
Extensibility through module imports is a primary goal of the NgModule system.
Merging module providers into the application injector
makes it easy for a module library to enrich the entire application with new services.
By adding the
HttpModule once, every application component can make http requests.
However, this might feel like an unwelcome surprise if you expect the module's services
to be visible only to the components declared by that feature module.
HeroModule provides the
HeroService and the root
any class that knows the
HeroService type can inject that service,
not just the classes declared in the
Why is a service provided in a lazy-loaded module visible only to that module?
Unlike providers of the modules loaded at launch, providers of lazy-loaded modules are module-scoped.
When the Angular router lazy-loads a module, it creates a new execution context. That context has its own injector, which is a direct child of the application injector.
The router adds the lazy module's providers and the providers of its imported modules to this child injector.
These providers are insulated from changes to application providers with the same lookup token. When the router creates a component within the lazy-loaded context, Angular prefers service instances created from these providers to the service instances of the application root injector.
What if two modules provide the same service?
When two imported modules, loaded at the same time, list a provider with the same token, the second module's provider "wins". That's because both providers are added to the same injector.
When Angular looks to inject a service for that token, it creates and delivers the instance created by the second provider.
Every class that injects this service gets the instance created by the second provider. Even classes declared within the first module get the instance created by the second provider.
If Module A provides a service for token 'X' and imports a module B that also provides a service for token 'X', then Module A's service definition "wins".
The service provided by the root
AppModule takes precedence over services provided by imported modules.
AppModule always wins.
How do I restrict service scope to a module?
When a module is loaded at application launch,
@NgModule.providers have application-wide scope;
that is, they are available for injection throughout the application.
Imported providers are easily replaced by providers from another imported module. Such replacement might be by design. It could be unintentional and have adverse consequences.
As a general rule, import modules with providers exactly once, preferably in the application's root module. That's also usually the best place to configure, wrap, and override them.
Suppose a module requires a customized
HttpBackend that adds a special header for all Http requests.
If another module elsewhere in the application also customizes
or merely imports the
HttpModule, it could override this module's
losing the special header. The server will reject http requests from this module.
To avoid this problem, import the
HttpModule only in the
AppModule, the application root module.
If you must guard against this kind of "provider corruption", don't rely on a launch-time module's
Load the module lazily if you can. Angular gives a lazy-loaded module its own child injector. The module's providers are visible only within the component tree created with this injector.
If you must load the module eagerly, when the application starts, provide the service in a component instead.
Continuing with the same example, suppose the components of a module truly require a private, custom
Create a "top component" that acts as the root for all of the module's components.
Add the custom
HttpBackend provider to the top component's
providers list rather than the module's
Recall that Angular creates a child injector for each component instance and populates the injector
with the component's own providers.
When a child of this component asks for the
Angular provides the local
not the version provided in the application root injector.
Child components make proper http requests no matter what other modules do to
Be sure to create module components as children of this module's top component.
You can embed the child components in the top component's template.
Alternatively, make the top component a routing host by giving it a
Define child routes and let the router load module components into that outlet.
Should I add application-wide providers to the root AppModule or the root AppComponent?
Register application-wide providers in the root
AppModule, not in the
Lazy-loaded modules and their components can inject
they can't inject
Register a service in
AppComponent providers only if the service must be hidden
from components outside the
AppComponent tree. This is a rare use case.
More generally, prefer registering providers in modules to registering in components.
Angular registers all startup module providers with the application root injector. The services created from root injector providers are available to the entire application. They are application-scoped.
Certain services (such as the
Router) only work when registered in the application root injector.
By contrast, Angular registers
AppComponent providers with the
AppComponent's own injector.
AppComponent services are available only to that component and its component tree.
They are component-scoped.
AppComponent's injector is a child of the root injector, one down in the injector hierarchy.
For applications that don't use the router, that's almost the entire application.
But for routed applications, "almost" isn't good enough.
AppComponent services don't exist at the root level where routing operates.
Lazy-loaded modules can't reach them.
In the NgModule page sample applications, if you had registered
UserService in the
HeroComponent couldn't inject it.
The application would fail the moment a user navigated to "Heroes".
Should I add other providers to a module or a component?
In general, prefer registering feature-specific providers in modules (
to registering in components (
Register a provider with a component when you must limit the scope of a service instance to that component and its component tree. Apply the same reasoning to registering a provider with a directive.
For example, a hero editing component that needs a private copy of a caching hero service should register
HeroService with the
Then each new instance of the
HeroEditorComponent gets its own cached service instance.
The changes that editor makes to heroes in its service don't touch the hero instances elsewhere in the application.
Always register application-wide services with the root
not the root
Why is it bad if SharedModule provides a service to a lazy-loaded module?
This question is addressed in the Why UserService isn't shared
section of the NgModules page,
which discusses the importance of keeping providers out of the
UserService was listed in the module's
providers (which it isn't).
Suppose every module imports this
SharedModule (which they all do).
When the app starts, Angular eagerly loads the
AppModule and the
Both instances of the imported
SharedModule would provide the
Angular registers one of them in the root app injector (see What if I import the same module twice?).
Then some component injects
UserService, Angular finds it in the app root injector,
and delivers the app-wide singleton
UserService. No problem.
Now consider the
HeroModule which is lazy loaded.
When the router lazy loads the
HeroModule, it creates a child injector and registers the
provider with that child injector. The child injector is not the root injector.
When Angular creates a lazy
HeroComponent, it must inject a
This time it finds a
UserService provider in the lazy module's child injector
and creates a new instance of the
This is an entirely different
than the app-wide singleton version that Angular injected in one of the eagerly loaded components.
That's almost certainly a mistake.
To demonstrate, run the
SharedModule so that it provides the
UserService rather than the
Then toggle between the "Contact" and "Heroes" links a few times.
The username goes bonkers as the Angular creates a new
UserService instance each time.
Why does lazy loading create a child injector?
@NgModule.providers to the application root injector, unless the module is lazy loaded.
For a lazy-loaded module, Angular creates a child injector and adds the module's providers to the child injector.
This means that a module behaves differently depending on whether it's loaded during application start or lazy loaded later. Neglecting that difference can lead to adverse consequences.
Why doesn't Angular add lazy-loaded providers to the app root injector as it does for eagerly loaded modules?
The answer is grounded in a fundamental characteristic of the Angular dependency-injection system. An injector can add providers until it's first used. Once an injector starts creating and delivering services, its provider list is frozen; no new providers are allowed.
When an applications starts, Angular first configures the root injector with the providers of all eagerly loaded modules before creating its first component and injecting any of the provided services. Once the application begins, the app root injector is closed to new providers.
Time passes and application logic triggers lazy loading of a module. Angular must add the lazy-loaded module's providers to an injector somewhere. It can't added them to the app root injector because that injector is closed to new providers. So Angular creates a new child injector for the lazy-loaded module context.
How can I tell if a module or service was previously loaded?
Some modules and their services should be loaded only once by the root
Importing the module a second time by lazy loading a module could produce errant behavior
that may be difficult to detect and diagnose.
To prevent this issue, write a constructor that attempts to inject the module or service from the root app injector. If the injection succeeds, the class has been loaded a second time. You can throw an error or take other remedial action.
Certain NgModules (such as
BrowserModule) implement such a guard,
such as this
CoreModule constructor from the NgModules page.
What is an entry component?
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 isn't a component template and the
selector doesn't match an element in any component template.
AppComponent dynamically because it's either listed by type in
or boostrapped imperatively with the module's
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
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
Angular automatically adds the following types of components to the module's
- The component in the
- Components referenced in router configuration.
You don't have to mention these components explicitly, although doing so is harmless.
What's the difference between a bootstrap component and an entry component?
A bootstrapped component is an entry component that Angular loads into the DOM during the bootstrap (application launch) process. Other entry components are loaded dynamically by other means, such as with the router.
@NgModule.bootstrap property tells the compiler that this is an entry component and
it should generate code to bootstrap the application with this component.
There's no need to list a component in both the
although doing so is harmless.
When do I add components to entryComponents?
Most application developers won't need to add components to the
Angular adds certain components to entry components automatically.
Components listed in
@NgModule.bootstrap are added automatically.
Components referenced in router configuration are added automatically.
These two mechanisms account for almost all entry components.
If your app happens to bootstrap or dynamically load a component by type in some other manner,
you must add it to
Although it's harmless to add components to this list, it's best to add only the components that are truly entry components. Don't include components that are referenced in the templates of other components.
Why does Angular need entryComponents?
Entry components are also declared.
Why doesn't the Angular compiler generate code for every component in
Then you wouldn't need entry components.
The reason is tree shaking. For production apps you want to load the smallest, fastest code possible. The code should contain only the classes that you actually need. It should exclude a component that's never used, whether or not that component is declared.
In fact, many libraries declare and export components you'll never use. If you don't reference them, the tree shaker drops these components from the final code package.
If the Angular compiler generated code for every declared component, it would defeat the purpose of the tree shaker.
Instead, the compiler adopts a recursive strategy that generates code only for the components you use.
The compiler starts with the entry components, then it generates code for the declared components it finds in an entry component's template, then for the declared components it discovers in the templates of previously compiled components, and so on. At the end of the process, the compiler has generated code for every entry component and every component reachable from an entry component.
If a component isn't an entry component or wasn't found in a template, the compiler omits it.
What kinds of modules should I have and how should I use them?
Every app is different. Developers have various levels of experience and comfort with the available choices. Some suggestions and guidelines appear to have wide appeal.
The following is preliminary guidance based on early experience using NgModules in a few applications. Read with appropriate caution and reflection.
SharedModule with the components, directives, and pipes that you use
everywhere in your app. This module should consist entirely of
most of them exported.
SharedModule may re-export other widget modules, such as
FormsModule, and modules with the UI controls that you use most widely.
SharedModule should not have
providers for reasons explained previously.
Nor should any of its imported or re-exported modules have
If you deviate from this guideline, know what you're doing and why.
SharedModule in your feature modules,
both those loaded when the app starts and those you lazy load later.
providers for the singleton services you load when the application starts.
CoreModule in the root
CoreModule in any other module.
CoreModule a pure services module with no
This page sample departs from that advice by declaring and exporting two components that are
only used within the root
AppComponent declared by
Someone following this guideline strictly would have declared these components in the
Create feature modules around specific application business domains, user workflows, and utility collections.
Feature modules tend to fall into one of the following groups:
- Domain feature modules.
- Routed feature modules.
- Routing modules.
- Service feature modules.
- Widget feature modules.
Real-world modules are often hybrids that purposefully deviate from the following guidelines. These guidelines are not laws; follow them unless you have a good reason to do otherwise.
Domain feature modules deliver a user experience dedicated to a particular application domain like editing a customer or placing an order.
They typically have a top component that acts as the feature root. Private, supporting sub-components descend from it.
Domain feature modules consist mostly of declarations. Only the top component is exported.
Domain feature modules rarely have providers. When they do, the lifetime of the provided services should be the same as the lifetime of the module.
Don't provide application-wide singleton services in a domain feature module.
Domain feature modules are typically imported exactly once by a larger feature module.
They might be imported by the root
Routed feature modules are domain feature modules whose top components are the targets of router navigation routes.
All lazy-loaded modules are routed feature modules by definition.
Routed feature modules shouldn't export anything. They don't have to because their components never appear in the template of an external component.
A lazy-loaded routed feature module should not be imported by any module.
Doing so would trigger an eager load, defeating the purpose of lazy loading.
But an eager loaded routed feature module must be imported by another module
so that the compiler learns about its components.
Routed Feature Modules rarely have providers for reasons explained earlier. When they do, the lifetime of the provided services should be the same as the lifetime of the module.
Don't provide application-wide singleton services in a routed feature module or in a module that the routed module imports.
A routing module provides routing configuration for another module.
A routing module separates routing concerns from its companion module.
A routing module typically does the following:
The name of the routing module should parallel the name of its companion module, using the suffix "Routing".
If the companion module is the root
A routing module re-exports the
A routing module should not have its own
A routing module should only be imported by its companion module.
Service modules provide utility services such as data access and messaging.
Ideally, they consist entirely of providers and have no declarations.
Service Modules should only be imported by the root
Do not import service modules in other feature modules. If you deviate from this guideline, know what you're doing and why.
A widget module makes components, directives, and pipes available to external modules.
A widget module should consist entirely of declarations, most of them exported.
A widget module should rarely have providers. If you deviate from this guideline, know what you're doing and why.
Import widget modules in any module whose component templates need the widgets.
The following table summarizes the key characteristics of each feature module group.
Real-world modules are often hybrids that knowingly deviate from these guidelines.
|Feature Module||Declarations||Providers||Exports||Imported By||Examples|
|Domain||Yes||Rare||Top component||Feature, |
|Routing||No||Yes (Guards)||Feature (for routing)|
(see the Modules page of the Exploring ES6 website).
Within each file you write an
export statement to make parts of the module public:
import a part in another module:
An NgModule is a feature of Angular itself.
NgModule also has
exports and they serve a similar purpose.
You import other NgModules so you can use their exported classes in component templates. You export this NgModule's classes so they can be imported and used by components of other modules.
- An NgModule bounds declarable classes only. Declarables are the only classes that matter to the Angular compiler.
you list the module's classes in the
- An NgModule can only export the declarable classes it owns or imports from other modules. It doesn't declare or export any other kind of class.
The NgModule is also special in another way.
by adding providers to the
The provided services don't belong to the module nor are they scoped to the declared classes. They are available everywhere.
Here's an NgModule class with imports, exports, and declarations.
How does Angular find components, directives, and pipes in a template?
What is a template reference?
The Angular compiler looks inside component templates for other components, directives, and pipes. When it finds one, that's a "template reference".
The Angular compiler finds a component or directive in a template when it can match the selector of that component or directive to some HTML in that template.
The compiler finds a pipe if the pipe's name appears within the pipe syntax of the template HTML.
Angular only matches selectors and pipe names for classes that are declared by this module or exported by a module that this module imports.
What is the Angular compiler?
@NgModule metadata play an important role in guiding the compilation process.
The code you write isn't immediately executable. Consider components. Components have templates that contain custom elements, attribute directives, Angular binding declarations, and some peculiar syntax that clearly isn't native HTML.
The Angular compiler reads the template markup, combines it with the corresponding component class code, and emits component factories.
of the component that incorporates everything described in its
the HTML, the binding instructions, the attached styles.
Because directives and pipes appear in component templates, the Angular compiler incorporates them into compiled component code too.
@NgModule metadata tells the Angular compiler what components to compile for this module and
how to link this module with other modules.
The following table summarizes the
NgModule metadata properties.
A list of declarable classes, the component, directive, and pipe classes that belong to this module.
These declared classes are visible within the module but invisible to components in a different module unless they are exported from this module and the other module imports this one.
Components, directives, and pipes must belong to exactly one module. The compiler emits an error if you try to declare the same class in more than one module.
Do not re-declare a class imported from another module.
A list of dependency-injection providers.
Angular registers these providers with the root injector of the module's execution context. That's the application's root injector for all modules loaded when the application starts.
Angular can inject one of these provider services into any component in the application.
If this module or any module loaded at launch provides the
A lazy-loaded module has its own sub-root injector which typically is a direct child of the application root injector.
Lazy-loaded services are scoped to the lazy module's injector.
If a lazy-loaded module also provides the
Components in external modules continue to receive the instance created for the application root.
A list of supporting modules.
Specifically, the list of modules whose exported components, directives, or pipes are referenced by the component templates declared in this module.
A component template can reference another component, directive, or pipe when the referenced class is declared in this module or the class was imported from another module.
A component can use the
You can import many standard directives with the
A list of declarations—component, directive, and pipe classes—that an importing module can use.
Exported declarations are the module's public API.
A component in another module can reference this module's
Declarations are private by default.
If this module does not export
Importing a module does not automatically re-export the imported module's imports.
Module 'B' can't use
A module can list another module among its
Re-export makes module transitivity explicit.
If Module 'A' re-exports
A list of components that can be bootstrapped.
Usually there's only one component in this list, the root component of the application.
Angular can launch with multiple bootstrap components, each with its own location in the host web page.
A bootstrap component is automatically an
A list of components that are not referenced in a reachable component template.
Most developers never set this property. The Angular compiler must know about every component actually used in the application. The compiler can discover most components by walking the tree of references from one component template to another.
But there's always at least one component that's not referenced in any template:
the root component,
Routed components are also entry components because they aren't referenced in a template either.
The router creates them and drops them into the DOM near a
While the bootstrapped and routed components are entry components,
you usually don't have to add them to a module's
Angular automatically adds components in the module's
That leaves only the following sources of undiscoverable components:
Both are advanced techniques that few developers ever employ.
If you are one of those few, you must add these components to the