Open In App

Purpose of NgModule Decorator in Angular

The NgModule decorator in Angular is like a blueprint for organizing and configuring different parts of your application. It's like a set of instructions that tells Angular how to assemble the various components, directives, pipes, and services into cohesive units called modules. These modules help in managing dependencies, defining routing rules, and configuring providers.

Essentially, the NgModule decorator serves as a central hub for defining and organizing the building blocks of an Angular application, making it easier to develop, maintain, and scale complex web applications.

Angular NgModules are also classes with ‘@NgModule’ and it has metadata that describes how the different parts of the application will work together. Like javascript modules have their own files but NgModule does not have their own file, they only include metadata. it also helps to organize the components and services of the angular application.

Syntax:

import { NgModule } from '@angular/core';

@NgModule({
declarations: [ /* Components, directives, and pipes */],
imports: [ /* Other modules */],
exports: [ /* Exports of this module */],
providers: [ /* Services */],
bootstrap: [ /* Root component */]
})

export class MyNgModule { }

Here, declarations is the array of components, directives, and pipes that belong to this module, imports is the array of other modules that this module depends on, exports is the array of declarations that should be visible and accessible to components in other modules, providers are the array of services available for injection within this module and bootstrap is the array containing the root component(s) to bootstrap when this module is loaded.

Features of NgModules:

Purpose of NgModule Decorator

Steps to create app:

Step 1: Create a new Angular project

ng new my-angular-app

Step 2: Navigate to the project directory

cd my-angular-app

Step 3: Serve the application

ng serve

Project Structure:

Screenshot-2024-03-20-164603

The updated dependencies in package.json file will look like.

"dependencies": {
"@angular/animations": "^16.0.0",
"@angular/common": "^16.0.0",
"@angular/compiler": "^16.0.0",
"@angular/core": "^16.0.0",
"@angular/forms": "^16.0.0",
"@angular/platform-browser": "^16.0.0",
"@angular/platform-browser-dynamic": "^16.0.0",
"@angular/router": "^16.0.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
}

Example:

<!-- app.component.html -->

<div>
    <h1 style="text-align: center; color: green">GeeksforGeeks</h1>
    <div style="margin-left: 20px">
        <h2>About Us</h2>
        <p>
            <strong>1. Company Profile and Brand:</strong><br />
            GeeksforGeeks is a leading platform that provides 
            computer science resources and coding challenges for
            programmers and technology enthusiasts, along with 
            interview and exam preparations for upcoming aspirants.
            With a strong emphasis on enhancing coding skills and 
            knowledge, it has become a trusted destination for over
            12 million plus registered users worldwide. The platform 
            offers a vast collection of tutorials, practice problems, 
            interview tutorials, articles, and courses,
            covering various domains of computer science.
            <br /><br />
            Our exceptional mentors hailing from top colleges 
            & organizations have the ability to guide you on a
            journey from the humble beginnings of coding to the 
            pinnacle of expertise. Under their guidance watch your 
            skills flourish as we lay the foundation and help 
            you conquer the world of coding.
        </p>
        <p>
            <strong>2. Company Founders/Directors:</strong><br />
            Our founder Sandeep Jain is a visionary entrepreneur and
            esteemed computer science expert. Fueled by his 
            unwavering passion for coding and education,
            laid the very bedrock upon which GeeksforGeeks stands 
            today, and his indomitable spirit has been instrumental
            in its remarkable growth and resounding success. As 
            the steadfast driving force behind the company,
            Sandeep remains a beacon of guidance and inspiration,
            propelling the teamto constantly challenging
            limits and craft transformative learning
            experiences.
        </p>
    </div>
</div>
//app.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
}
//app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
    declarations: [AppComponent],
    imports: [BrowserModule],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

Screenshot-2024-03-20-164303-min

Article Tags :