Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Angular Modules and NgModules

Introduction

In Angular, modules are a fundamental building block. They help organize an application into cohesive blocks of functionality. NgModules are an essential part of Angular's architecture and offer a way to group components, directives, pipes, and services into cohesive blocks of functionality.

What are NgModules?

An NgModule is a class marked by the @NgModule decorator. It organizes an application into cohesive blocks of functionality, encapsulating components, directives, pipes, and services.

Note: Each Angular application has at least one root module, typically named AppModule.

Key Properties of NgModules

  • Declarations: The components, directives, and pipes that belong to this module.
  • Imports: Other modules whose exported classes are needed by component templates in this module.
  • Exports: The subset of declarations that should be visible and usable in the component templates of other modules.
  • Providers: Services that the module contributes to the global collection of services; they become accessible in all parts of the application.
  • Bootstrap: The main application view that hosts the root component. Only the root module can set this property.

Creating an NgModule

To create an NgModule, you need to define a class and decorate it with the @NgModule decorator. Here’s a simple example:


import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }
                

Using NgModules

To use an NgModule, you need to import it into the root module or any other module that requires it. You can also create feature modules to encapsulate specific functionalities.

Example of Feature Module


import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FeatureComponent } from './feature.component';

@NgModule({
    declarations: [FeatureComponent],
    imports: [CommonModule],
    exports: [FeatureComponent]
})
export class FeatureModule { }
                

To use FeatureModule in your main application module, import it as follows:


import { FeatureModule } from './feature/feature.module';

@NgModule({
    imports: [
        BrowserModule,
        FeatureModule
    ]
})
export class AppModule { }
                

Best Practices

  • Keep NgModules focused on a single purpose, such as a feature or a specific functionality.
  • Group related components, services, and pipes within their respective NgModules.
  • Avoid circular dependencies between modules.
  • Use shared modules for commonly used components and services.
  • Lazy load feature modules for better performance and reduced initial load time.

FAQ

What is the purpose of NgModules?

NgModules help organize an Angular application into cohesive blocks of functionality, encapsulating related components, directives, pipes, and services.

Can I have multiple NgModules in an Angular application?

Yes, you can have multiple NgModules to organize your application better. It’s common to create feature modules for specific functionalities.

What is a shared module?

A shared module is a module that exports common components, directives, and pipes for use across other modules. It typically imports CommonModule and is imported by feature modules.