NgModule API
Prerequisites
A basic understanding of the following concepts:
Purpose of @NgModule
At a high level, NgModules are a way to organize Angular apps and they accomplish this through the metadata in the @NgModule
decorator. The metadata falls into three categories:
-
Static: Compiler configuration which tells the compiler about directive selectors and where in templates the directives should be applied through selector matching. This is configured via the
declarations
array. -
Runtime: Injector configuration via the
providers
array. -
Composability/Grouping: Bringing NgModules together and making them available via the
imports
andexports
arrays.
@NgModule({ // Static, that is compiler configuration declarations: [], // Configure the selectors entryComponents: [], // Generate the host factory // Runtime, or injector configuration providers: [], // Runtime injector configuration // Composability / Grouping imports: [], // composing NgModules together exports: [] // making NgModules available to other parts of the app })
@NgModule
metadata
The following table summarizes the @NgModule
metadata properties.
Property | Description |
---|---|
declarations | A list of declarable classes, (components, directives, and pipes) that belong to this module.
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. Be careful not to re-declare a class that is imported directly or indirectly from another module. |
providers | A list of dependency-injection providers. Angular registers these providers with the NgModule's injector. If it is the NgModule used for bootstrapping then it is the root injector. These services become available for injection into any component, directive, pipe or service which is a child of this injector. A lazy-loaded module has its own injector which is typically a 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 provided by their injectors. For more information on injector hierarchy and scoping, see Providers and the DI Guide. |
imports | A list of modules which should be folded into this module. Folded means it is as if all the imported NgModule's exported properties were declared here. Specifically, it is as if the list of modules whose exported components, directives, or pipes are referenced by the component templates were declared in this module. A component template can reference another component, directive, or pipe when the reference is declared in this module or if the imported module has exported it. For example, a component can use the You can import many standard directives from the |
exports | 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 use 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 |
bootstrap | A list of components that are automatically 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 added to |
entryComponents | A list of components that can be dynamically loaded into the view. By default, an Angular app always has at least one entry component, the root component, Routed components are also entry components because they need to be loaded dynamically. The router creates them and drops them into the DOM near a While the bootstrapped and routed components are entry components, you don't have to add them to a module's Angular automatically adds components in the module's That leaves only components bootstrapped using one of the imperative techniques, such as Dynamic component loading is not common in most apps beyond the router. If you need to dynamically load components, you must add these components to the For more information, see Entry Components. |
More on NgModules
You may also be interested in the following:
© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v6.angular.io/guide/ngmodule-api