Skip to main content

What does CommonModule do?

In Angular, CommonModule is a module that provides the basic directives and pipes used in templates, except for those available in the root module (BrowserModule).

Key points

  1. Directives:
  • NgIf - conditional display of elements.
  • NgForOf - repeating collection items (*ngFor).
  • NgSwitch, NgSwitchCase, NgSwitchDefault - controlling display based on a value.
  1. Pipes:
  • DatePipe - formats dates.
  • CurrencyPipe - formats currency.
  • UpperCasePipe, LowerCasePipe - change text case.
  • JsonPipe - converts objects into a JSON string.
  • Other standard pipes for working with numbers, dates, and strings.
  1. Usage:
  • Imported in feature/user modules instead of BrowserModule, which is imported only in AppModule.
ts
import { CommonModule } from '@angular/common'; @NgModule({ imports: [CommonModule], declarations: [MyComponent], }) export class MyFeatureModule {}

In other words, CommonModule provides a standard set of directives and pipes for working with templates in any module except the root one.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.