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
- Directives:
NgIf- conditional display of elements.NgForOf- repeating collection items (*ngFor).NgSwitch,NgSwitchCase,NgSwitchDefault- controlling display based on a value.
- 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.
- Usage:
- Imported in feature/user modules instead of
BrowserModule, which is imported only inAppModule.
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 readyPremium
A concise answer to help you respond confidently on this topic during an interview.