Return to site

ANGULAR INTERVIEW QUESTION: Can you explain how works the @NgModule decorator?

November 12, 2021

First, let's define what is an Angular Module:

𝐀𝐧 𝐀𝐧𝐠ðŪðĨ𝐚ðŦ ðĶðĻ𝐝ðŪðĨ𝐞 𝐠ðŦðĻðŪðĐ𝐎 𝐝ðĒ𝐟𝐟𝐞ðŦ𝐞𝐧𝐭 ðĐðŦðĻðĢ𝐞𝐜𝐭𝐎 ðĒ𝐭𝐞ðĶ𝐎 ðĨðĒðĪ𝐞 𝐜ðĻðĶðĐðĻ𝐧𝐞𝐧𝐭𝐎, 𝐝ðĒðŦ𝐞𝐜𝐭ðĒðŊ𝐞𝐎, 𝐎𝐞ðŦðŊðĒ𝐜𝐞𝐎, ðĐðĒðĐ𝐞𝐎 𝐭ðĻ 𝐟ðŪðĨðĨ𝐟ðĒðĨðĨ 𝐚 𝐎ðĐ𝐞𝐜ðĒ𝐟ðĒ𝐜 ðĐðŦðĻðĢ𝐞𝐜𝐭 𝐟𝐞𝐚𝐭ðŪðŦ𝐞.

Technically, a module is a class bound to a decorator @NgModule, allowing to describe how the app items box ðŸ“Ķ between themselves.

Below the code of a module class:

In "ð‘–ð‘šð‘ð‘œð‘Ÿð‘Ąð‘ ": we put all the modules required by this module.

In "𝑒ð‘Ĩð‘ð‘œð‘Ÿð‘Ąð‘ ", we expose the items of this module so that other modules can import them.

In "ð‘‘ð‘’ð‘ð‘™ð‘Žð‘Ÿð‘Žð‘Ąð‘–ð‘œð‘›ð‘ ", we list all the elements of the module, you need to declare your items so that Angular recognizes it.

In "𝑝𝑟𝑜ð‘Ģ𝑖𝑑𝑒𝑟𝑠", we put the service to inject for that module.

In "ð‘ð‘œð‘œð‘Ąð‘ ð‘Ąð‘Ÿð‘Žð‘", we put the component that is first loaded by the app; this property is only used for the "ð‘Ÿð‘œð‘œð‘Ą" module "ðī𝑝𝑝𝑀𝑜𝑑ð‘Ē𝑙𝑒.ð‘Ąð‘ "