What is the AppModule in Angular ?
Last Updated :
04 Apr, 2024
In Angular, AppModule plays an important role as the entry point to an Angular application. In this article, we'll learn about what AppModule is, its structure, and its significance in Angular applications. We'll also look at some examples to have a clear understanding.
What is AppModule?
In Angular, the AppModule
is a core module that serves as the root module of an Angular application. The AppModule
defines the structure, bootstrapping process, and initialization logic necessary for Angular to run the application.
An AppModule can also be considered the heart of every Angular application. It is typically found in the app.module.ts file and serves as the entry point to the application. Within the AppModule we declare and configure the different parts of our application like components, directives, pipes, services, and other modules.
Syntax:
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
Structure of AppModule:
The AppModule class is decorated by a '@NgModule' decorator. This decorator identifies the AppModule class as the Angular module class and provides the meta data required to compile and run the application.
Let's have a look at the various properties of @NgModule decorator:
- Imports: The Imports array contains all other modules required for the application. These could be Angular modules like BrowserModule, FormsModule or custom feature modules.
- Declarations: Declarations array contains components, directives, and pipes specific to the AppModule. By declaring them within the AppModule Declarations section, we make them available for use throughout the application.
- Providers: Providers array contains the services or dependencies required by the application. These services can be injected into the components, directives or other services.
- Bootstrap: The bootstrap array specifies the root component that angular should bootstrap when the application starts. This typically contains the root component AppComponent.
Importance of AppModule
AppModule plays a important role in Angular applications for several reasons:
- It provides a structured way to organize and configure components, services and other parts of the application.
- AppModules provides modularity by allowing the import of different modules in the application.
- It serves as the entry point for Angular application, defining the application's initial configuration and setup.
- AppModule provides the dependency injection system allowing seamless sharing of dependencies across the application.
Examples of AppModule in Angular
Example 1: In the example, we'll create a simple Angular application with a single component declared in the AppModule.
Step 1: Create a new Angular project using the Angular CLI
ng new my-app
Step 2: Navigate to the project directory.
cd my-app
Folder Structure:
Project StructureDependencies:
"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/router": "^17.3.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Step 3: Add the following codes in the respective files.
JavaScript
//app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: '<h1>Welcome to GeeksForGeeks!</h1>',
styleUrls: ['./app.component.css'],
})
export class AppComponent { }
JavaScript
//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 { }
Step 4: Save the changes and run the application using the Angular CLI.
ng serve
Output:
Output for example 1Example 2: In this example, we'll create a simple custom module and import it into the imports section of AppModule.
Step 1: create a new Angular project using the Angular CLI.
ng new custom-module-example
Step 2: Navigate to the project directory.
cd custom-module-example
Step 3: Create a custom module using the below command.
ng generate module custom-module
Step 4: Create a simple component within the newly created module.
ng generate component custom-module/custom-component
Step 5: Open the custom-module.module.ts file located in src/app/custom-module directory and export our custom component.
HTML
<!--app.component.html-->
<app-custom-component></app-custom-component>
JavaScript
//custom-module.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CustomComponentComponent } from './custom-component/custom-component.component';
@NgModule({
declarations: [],
imports: [CommonModule, CustomComponentComponent],
exports: [CustomComponentComponent],
})
export class CustomModuleModule { }
JavaScript
//custom-component.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-custom-component',
standalone: true,
imports: [],
template: '<h1>This is my custom component!</h1>',
styleUrl: './custom-component.component.css'
})
export class CustomComponentComponent { }
JavaScript
//app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { CustomModuleModule } from './custom-module/custom-module.module';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, CustomModuleModule],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
Step 6: Save the changes and run the application.
ng serve
Output:
Output for example 2
Similar Reads
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 applicat
2 min read
What is the factory function in Angular ?
In Angular, the factory function is always inclined towards class and constructors. Generally, the factory function is used to provide service as a dependency in any angular application. A factory function generates an object, provides it with some logic, executes the function, and returns the objec
4 min read
What are angular Material Icons ?
Angular Material is a UI component library which is developed by Google so that Angular developers can develop modern applications in a structured and responsive way. By making use of this library, we can greatly increase the user experience of an end-user thereby gaining popularity for our applicat
3 min read
What is AOT and JIT Compiler in Angular ?
An angular application mainly consists of HTML templates, their components which include various TypeScript files. There are some unit testing and configuration file. Whenever we run over an application, the browser cannot understand the code directly hence we have to compile our code. What is Ahead
6 min read
What is APP_BASE_HREF in Angular 10 ?
In this article, we are going to see what is APP_BASE_HREF in Angular 10 and how to use it. APP_BASE_HREF returns a predefined DI token for the base href of the current page. The APP_BASE_HREF is the URL prefix that should be preserved. Syntax: Â provide: APP_BASE_HREF, useValue: '/gfgapp' Approach:
1 min read
What is Angular Expression ?
Angular is a great, reusable UI (User Interface) library for developers that help in building attractive, and steady web pages and web application. In this article, we will learn about Angular expression. Table of Content Angular ExpressionDifferent Use Cases of Angular ExpressionsSyntaxApproach Ang
4 min read
What is NgStyle in Angular 10 ?
In this article, we are going to see what is NgStyle in Angular 10 and how to use it. NgStyle is used to add some style to an HTML element Syntax: <element [ngStyle] = "typescript_property"> Approach:Â Create the Angular app to be usedIn app.component.html make an element and sets its class us
1 min read
Purpose of the FormsModule in Angular
Forms are widely used in web applications that allow you to provide the data, submit the forms, and interact with the application. In Angular for handling the forms, we use Forms Module which imports several powerful tools for creating, managing, and validating forms. In this article, we'll cover th
5 min read
What is Angular Router?
Angular Router is an important feature of the Angular framework that helps you to build rich and interactive single-page applications (SPAs) with multiple views. In this article, we'll learn in detail about Angular Router. PrerequisitesNPM or Node jsBasics of AngularBasics of RoutingCode editor ( e.
5 min read
What are templates in AngularJS ?
Templates in AngularJS are simply HTML files filled or enriched with AngularJS stuff like attributes and directives. A directive is a marker element that is used to target a particular attribute or class to render its behavior according to the needs. Model and controller in Angular are combined with
3 min read