13

I am just starting to learn Angular. I have come across several examples using ngModule decorators over an empty class.

What I want to know is... Is the class declared directly below the ngModule decorator only used to initialize a module or does it serve any other purpose?

Zze
  • 18,229
  • 13
  • 85
  • 118
Devendiran Kumar
  • 223
  • 3
  • 12

4 Answers4

5

You can for example use the constructor or implement ngDoBootstrap to execute code when a lazy loaded module was loaded the first time (initialized)

You can also inject services to the constructor so that they are instantiated by DI when the application or the lazy loaded module is initialized.

I can't remember seeing other examples. Besides that it only carries the @NgModule() decorator and the information provided there. (There might be some additional code generated when the app is built)

See also the ngDoBootstrap comment in https://angular.io/guide/entry-components

yurzui
  • 205,937
  • 32
  • 433
  • 399
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
2

you need the class declaration for the effective export. The module is initialized with imports, but you have access to functionalities from the class export (in fact, what are you importing?)

Hope this helped

EDIT --

"The body of the class is empty. But that decorator above the class (@NgModule) is giving that class its functionality. So really, that class isn't empty. It just doesn't require any extra logic after the decorator is applied to it. bootstrapModule takes a class as input and assumes that that class is decorated with @NgModule configured in a manner similar to what you have (declarations, imports, providers, etc.)." from: Why Angular export empty class in app.modules.ts?

Luca Taccagni
  • 1,059
  • 6
  • 23
2

The class is only used as a reference you can import from typescript. The decorator cannot be put without a class or member to decorate.

After the compilation, it will hold the annotation you used to decorate that class. Those will be used by angular to configure the injector, especially which class it needs to create when a component needs to inject a given member type.

Now, you could put something into the constructor, for instance if you want to do something at the time the module (and related components / providers) are about to be injected. This is the only use I can think about right now.

[edit] I just used it to register a locale when the app do start.

export class AppModule {
    constructor() {
        registerLocaleData(localeDeCH);
    }
}

The idea came from here where they do not specify where to call registerLocaleData

Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
2

Take a look at the official documentation for angular NgModules. https://angular.io/guide/ngmodules

The following are from that:

@NgModule takes a metadata object that describes how to compile a component's templates and how to create an injector at runtime

NgModule metadata does the following:

  1. Declares which components, directives, and pipes belong to the module.
  2. Makes some of those components, directives, and pipes public so that other module's component templates can use them.
  3. Imports other modules with the components, directives, and pipes that components in the current module need. Provides
  4. services at the other application components can use.
Arun Gopinathan
  • 378
  • 4
  • 11