Open In App

What is CommonModule in Angular 10 ?

In this article, we are going to see what is CommonModule in Angular 10 and how to use it.

CommonModule is used to export all the basic Angular directives and pipes. It is re-exported when we import BrowserModule into our angular application, BrowserModule is automatically imported into our application when we create our Angular application using ‘ng new’ command



Syntax: CommonModule is automatically imported by BrowserModule when the app is created.

 import { BrowserModule } from '@angular/platform-browser';
 
 @NgModule({

  imports: [
    BrowserModule,
  ]
})
export class AppModule { }

Directives Imported:



Pipes Imported:

Approach: 

Example 1:




import { NgModule } from '@angular/core';
// BrowserModule automatically imports all CommonModule Dependencies
  
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    // Adding Imports
    BrowserModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }




import { Component, OnInit } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    geek = "GeekClass";
    g = document.getElementsByClassName(this.geek);
}




<!-- use of ngClass directive -->
<h1 [ngClass] = "geek">
  GeeksforGeeks
</h1>
  Upper Heading's class is : "{{ g[0].className }}"

Output:


Article Tags :