angular
/

Angular Feature Modules – Scaling Large Applications

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

angular

Angular Feature Modules – Scaling Large Applications

What is a Feature Module?

As your application grows, the Root Module (AppModule) can become a cluttered 'God Object' that is difficult to manage. A Feature Module is an NgModule that helps you partition the application into focused areas. As a Technical Lead, you use feature modules to group related components, directives, and pipes together. This creates clear boundaries between different domains of your app—like a 'User Dashboard' versus an 'Admin Panel'—and is the prerequisite for implementing Lazy Loading.

  1. Types of Feature Modules

Not all modules serve the same purpose. In a professional architecture, we categorize modules based on their responsibilities.

  • Domain Feature Modules: Deliver a specific user experience (e.g., EditorModule). They contain components, private services, and models related to that feature.
  • Routed Feature Modules: Domain modules whose top-level components are the targets of router navigation (used with loadChildren).
  • Service Modules: Provide utility services (e.g., CoreModule for authentication or logging). They usually only contain 'providers' and no declarations.
  • Widget/Shared Modules: Contain reusable components (buttons, spinners) and pipes that are imported by many other feature modules.

  1. Creating and Importing a Feature Module

When you create a feature module, you typically import CommonModule (which provides *ngIf and *ngFor) instead of BrowserModule (which belongs only in the root).

BASHRead-only
1
# Generate a feature module with its own routing
ng generate module features/dashboard --routing
TypeScriptRead-only
1
@NgModule({
  declarations: [DashboardComponent, SummaryWidgetComponent],
  imports: [
    CommonModule,
    DashboardRoutingModule
  ],
  exports: [DashboardComponent] // Export only what other modules need to see
})
export class DashboardModule { }

  1. The Shared Module Pattern

To avoid importing standard libraries (like Angular Material) into every single feature module, architects often create a SharedModule. This module imports and then re-exports common modules, making them available to any feature module that imports the SharedModule.

TypeScriptRead-only
1
@NgModule({
  imports: [CommonModule, MatButtonModule, MatIconModule],
  exports: [CommonModule, MatButtonModule, MatIconModule, CustomSpinnerComponent]
})
export class SharedModule { }

Module Communication Table

Module TypeImportsExportsProviders
Root (App)BrowserModule, FeatureModulesNoneGlobal App Services
FeatureCommonModule, SharedModuleMain Feature ComponentFeature-specific Services
SharedCommonModule, UI LibrariesUI Components & DirectivesAvoid (can cause duplicates)
CoreCommonModuleNoneSingleton App-wide Services

Test Your Knowledge

Q1
of 3

What is the primary benefit of organizing an app into Feature Modules?

A
It makes the CSS load faster
B
It allows for Lazy Loading and better code organization
C
It removes the need for TypeScript
D
It automatically connects to Firebase
Q2
of 3

Which module should be imported in a feature module to use directives like *ngIf?

A
BrowserModule
B
CommonModule
C
AppModule
D
FormsModule
Q3
of 3

In the Shared Module pattern, what must you do to a component so other modules can use it?

A
Declare it in 'providers'
B
Add it to the 'exports' array
C
Add it to the 'bootstrap' array
D
Nothing, it is shared automatically

Frequently Asked Questions

Should I put services in a SharedModule?

Generally, no. If a SharedModule is imported by multiple lazy-loaded modules, and it contains providers, each module might get its own instance of the service. Keep services in a 'CoreModule' or use 'providedIn: root' to ensure they remain singletons.

Can one feature module import another?

Yes, but be careful of circular dependencies (Module A imports B, and B imports A). If two modules need the same component, that component should probably be moved to a 'SharedModule'.

Why use CommonModule instead of BrowserModule in feature modules?

BrowserModule contains the code to launch (bootstrap) the application in the browser. You only want that to happen once in the Root Module. Feature modules only need common directives, which are provided by CommonModule.

Previous

angular modules

Next

angular shared modules

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.