W3cubDocs

/Angular

NgModule

decorator

Decorator that marks a class as an NgModule and supplies configuration metadata.

Option Description
providers?

The set of injectable objects that are available in the injector of this module.

declarations?

The set of components, directives, and pipes (declarables) that belong to this module.

imports?

The set of NgModules whose exported declarables are available to templates in this module.

exports?

The set of components, directives, and pipes declared in this NgModule that can be used in the template of any component that is part of an NgModule that imports this NgModule. Exported declarations are the module's public API.

bootstrap?

The set of components that are bootstrapped when this module is bootstrapped.

schemas?

The set of schemas that declare elements to be allowed in the NgModule. Elements and properties that are neither Angular components nor directives must be declared in a schema.

id?

A name or path that uniquely identifies this NgModule in getNgModuleById. If left undefined, the NgModule is not registered with getNgModuleById.

jit?

When present, this module is ignored by the AOT compiler. It remains in distributed code, and the JIT compiler attempts to compile it at run time, in the browser. To ensure the correct behavior, the app must import @angular/compiler.

Options

The set of injectable objects that are available in the injector of this module.

providers?: Array<Provider | EnvironmentProviders>

Dependencies whose providers are listed here become available for injection into any component, directive, pipe or service that is a child of this injector. The NgModule used for bootstrapping uses the root injector, and can provide dependencies to any part of the app.

A lazy-loaded module has its own injector, typically a child of the app root injector. Lazy-loaded services are scoped to the lazy-loaded module's injector. If a lazy-loaded module also provides the UserService, any component created within that module's context (such as by router navigation) gets the local instance of the service, not the instance in the root injector. Components in external modules continue to receive the instance provided by their injectors.

Example

The following example defines a class that is injected in the HelloWorld NgModule:

class Greeter {
   greet(name:string) {
     return 'Hello ' + name + '!';
   }
}

@NgModule({
  providers: [
    Greeter
  ]
})
class HelloWorld {
  greeter:Greeter;

  constructor(greeter:Greeter) {
    this.greeter = greeter;
  }
}

The set of components, directives, and pipes (declarables) that belong to this module.

declarations?: Array<Type<any> | any[]>

The set of selectors that are available to a template include those declared here, and those that are exported from imported NgModules.

Declarables 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 declare a class that is imported from another module.

Example

The following example allows the CommonModule to use the NgFor directive.

@NgModule({
  declarations: [NgFor]
})
class CommonModule {
}

The set of NgModules whose exported declarables are available to templates in this module.

imports?: Array<Type<any> | ModuleWithProviders<{}> | any[]>

A template can use exported declarables from any imported module, including those from modules that are imported indirectly and re-exported. For example, ModuleA imports ModuleB, and also exports it, which makes the declarables from ModuleB available wherever ModuleA is imported.

Example

The following example allows MainModule to use anything exported by CommonModule:

@NgModule({
  imports: [CommonModule]
})
class MainModule {
}

The set of components, directives, and pipes declared in this NgModule that can be used in the template of any component that is part of an NgModule that imports this NgModule. Exported declarations are the module's public API.

exports?: Array<Type<any> | any[]>

A declarable belongs to one and only one NgModule. A module can list another module among its exports, in which case all of that module's public declaration are exported.

Declarations are private by default. If this ModuleA does not export UserComponent, then only the components within this ModuleA can use UserComponent.

ModuleA can import ModuleB and also export it, making exports from ModuleB available to an NgModule that imports ModuleA.

Example

The following example exports the NgFor directive from CommonModule.

@NgModule({
  exports: [NgFor]
})
class CommonModule {
}

The set of components that are bootstrapped when this module is bootstrapped.

bootstrap?: Array<Type<any> | any[]>

The set of schemas that declare elements to be allowed in the NgModule. Elements and properties that are neither Angular components nor directives must be declared in a schema.

schemas?: Array<SchemaMetadata | any[]>

Allowed value are NO_ERRORS_SCHEMA and CUSTOM_ELEMENTS_SCHEMA.

A name or path that uniquely identifies this NgModule in getNgModuleById. If left undefined, the NgModule is not registered with getNgModuleById.

id?: string

When present, this module is ignored by the AOT compiler. It remains in distributed code, and the JIT compiler attempts to compile it at run time, in the browser. To ensure the correct behavior, the app must import @angular/compiler.

jit?: true

© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/core/NgModule