Sitemap
Ace D Baugh

Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer

Angular Modules

3 min readJan 2, 2023
Press enter or click to view image in full size
Photo by Volodymyr Hryshchenko on Unsplash

In Angular, a module is a way to organize code. It is a container for different parts of an application, such as controllers, services, filters, directives, and so on. Modules can be divided into two types:

Root module: This is the main module of the application, and it is defined in the main file of the application, usually called “app.module.ts”. The root module is responsible for bootstrapping the application, which means starting it and making it run.

Feature modules: These are modules that represent a specific feature of the application, such as a login module or a shopping cart module. Feature modules help to organize and manage the different parts of the application, and they can also be lazy-loaded, which means that they are only loaded when needed.

Modules in Angular are created using the NgModule decorator, which is a function that takes a single metadata object. The metadata object contains information about the module, such as its imports, declarations, providers, and exports.

Here is an example of a simple root module:

In this example, the root module imports the BrowserModule, which is a module provided by Angular that contains directives and services that are essential for running an Angular application in a web browser. The root module also declares the AppComponent, which is the main component of the application, and it bootstraps the AppComponent, which means that it is the first component to be loaded when the application starts.

Get Ace D Baugh’s stories in your inbox

Join Medium for free to get updates from this writer.

Modules can also import other modules and export components, directives, and pipes that can be used by other modules. For example, consider the following feature module:

In this example, the LoginModule imports the CommonModule, which is a module provided by Angular that contains directives and services that are commonly used in Angular applications. The LoginModule also declares the LoginComponent, which is a component that represents the login feature, and it provides the LoginService, which is a service that handles the login logic. Finally, the LoginModule exports the LoginComponent, which means that it can be used by other modules.

Modules are a powerful way to organize and manage the different parts of an Angular application. They help to keep the code clean and organized, and they also allow for lazy-loading, which can improve the performance of the application.

References

Angular. Introduction to Modules. https://angular.io/guide/architecture-modules

Fain, Y., & Moiseev, A. (2017). Angular 2 development with typescript. Manning Publications Co.

Mukherjee, A. (2022, Jan 10). Understanding Angular Modules. https://dev.to/this-is-angular/understanding-angular-modules-41pb

--

--

Ace D Baugh
Ace D Baugh

Written by Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer