Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of code we write inside class declared under ngModule decorator?

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?

like image 438
Devendiran Kumar Avatar asked Oct 07 '16 06:10

Devendiran Kumar


People also ask

Which of the classes should be included in declarations section of @NgModule?

link. Declarables are the class types —components, directives, and pipes— that you can add to a module's declarations list. They're the only classes that you can add to declarations .

Which decorator is applicable for NgModule class in Angular?

An NgModule is a class marked by the @NgModule decorator. @NgModule takes a metadata object that describes how to compile a component's template and how to create an injector at runtime.

What is NgModule decorator in Angular?

The @NgModule() decorator is a function that takes a single metadata object, whose properties describe the module. The most important properties are as follows. declarations: The components, directives, and pipes that belong to this NgModule.


1 Answers

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

like image 110
Günter Zöchbauer Avatar answered Oct 21 '22 14:10

Günter Zöchbauer