0
0
Angularframework~5 mins

Why modules organize applications in Angular

Choose your learning style9 modes available
Introduction

Modules help keep an Angular app neat and tidy by grouping related parts together. This makes the app easier to understand and manage.

When building a large app with many features to keep code organized.
When you want to reuse parts of your app in different places.
When you want to load parts of your app only when needed to make it faster.
When working with a team to separate work into clear sections.
When you want to keep related components, services, and pipes together.
Syntax
Angular
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SomeComponent } from './some.component';

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

@NgModule is a decorator that marks a class as a module.

declarations list components, directives, and pipes that belong to this module.

Examples
This is the main app module that starts the Angular app.
Angular
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule],
  bootstrap: [AppComponent]
})
export class AppModule {}
This module groups user-related components to keep them separate from other parts.
Angular
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UserProfileComponent } from './user-profile.component';

@NgModule({
  declarations: [UserProfileComponent],
  imports: [CommonModule],
  exports: [UserProfileComponent]
})
export class UserModule {}
Sample Program

This simple Angular app uses a module to organize the HelloComponent. The module tells Angular what to load and start.

Angular
import { Component } from '@angular/core';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

@Component({
  selector: 'app-hello',
  template: `<h1>Hello from HelloComponent!</h1>`
})
export class HelloComponent {}

@NgModule({
  declarations: [HelloComponent],
  imports: [BrowserModule],
  bootstrap: [HelloComponent]
})
export class AppModule {}
OutputSuccess
Important Notes

Modules help Angular know which parts belong together and what to load.

Always declare components inside a module to use them.

Modules can import other modules to share features.

Summary

Modules group related parts of an Angular app for better organization.

They make apps easier to manage, especially as they grow.

Modules help Angular know what to load and when.