The root module is the main starting point of an Angular app. It tells Angular what components and features to load first.
Root module (AppModule) structure in Angular
Introduction
Syntax
Angular
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 {}
@NgModule decorator defines the module's metadata.
declarations list components, directives, and pipes in this module.
Examples
Angular
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 {}
Angular
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HttpClientModule } from '@angular/common/http'; import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, HttpClientModule], providers: [], bootstrap: [AppComponent] }) export class AppModule {}
Angular
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouterModule } from '@angular/router'; import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, RouterModule.forRoot([])], providers: [], bootstrap: [AppComponent] }) export class AppModule {}
Sample Program
This is a complete root module with a simple component that shows a welcome message. The app starts by showing the AppComponent content.
Angular
import { Component } from '@angular/core'; import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; @Component({ selector: 'app-root', template: `<h1>Welcome to My Angular App!</h1>` }) export class AppComponent {} @NgModule({ declarations: [AppComponent], imports: [BrowserModule], providers: [], bootstrap: [AppComponent] }) export class AppModule {}
Important Notes
The bootstrap array tells Angular which component to load first.
Always import BrowserModule in the root module for browser apps.
Keep the root module simple; add features by importing other modules.
Summary
The root module organizes the app's main components and features.
Use @NgModule to declare components and import needed modules.
The bootstrap property starts the app with the main component.
Practice
1. What is the main purpose of the
AppModule in an Angular application?easy
Solution
Step 1: Understand the role of AppModule
The AppModule is the root module that organizes the app's components and imports other modules.Step 2: Identify what AppModule does
It declares components and imports modules needed for the app to work properly.Final Answer:
To declare components and import modules needed for the app -> Option AQuick Check:
AppModule organizes components and imports [OK]
Hint: AppModule declares components and imports modules [OK]
Common Mistakes:
- Confusing AppModule with styling or HTTP handling
- Thinking AppModule only manages routing
- Assuming AppModule is for CSS or HTTP only
2. Which of the following is the correct way to define the
bootstrap property in @NgModule for starting the app with AppComponent?easy
Solution
Step 1: Recall the syntax for bootstrap in @NgModule
The bootstrap property expects an array of components to start the app.Step 2: Match the correct syntax
Usingbootstrap: [AppComponent]is the correct way to provide an array with the main component.Final Answer:
bootstrap: [AppComponent] -> Option DQuick Check:
Bootstrap uses array syntax [OK]
Hint: Bootstrap needs an array of components [OK]
Common Mistakes:
- Using assignment (=) instead of colon (:)
- Not wrapping component in an array
- Omitting brackets around AppComponent
3. Given this
AppModule snippet, what will happen when the app starts?import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
bootstrap: [AppComponent]
})
export class AppModule { }medium
Solution
Step 1: Check declarations and imports
AppComponent is declared and BrowserModule is imported, which is required for browser apps.Step 2: Verify bootstrap property
AppComponent is bootstrapped correctly, so it will be the starting component shown.Final Answer:
The app will start and display the AppComponent template -> Option CQuick Check:
Proper declarations, imports, and bootstrap means app runs [OK]
Hint: Check declarations, imports, and bootstrap for app start [OK]
Common Mistakes:
- Assuming BrowserModule is missing
- Thinking bootstrap array can be empty
- Confusing syntax errors with correct code
4. Identify the error in this
AppModule code:@NgModule({
declarations: AppComponent,
imports: [BrowserModule],
bootstrap: [AppComponent]
})
export class AppModule { }medium
Solution
Step 1: Check declarations syntax
Declarations must be an array of components, but here it is a single component without brackets.Step 2: Confirm other parts are correct
Imports and bootstrap are correctly formatted; import statement is assumed present.Final Answer:
declarations should be an array, not a single component -> Option BQuick Check:
Declarations require array syntax [OK]
Hint: Declarations always use arrays [OK]
Common Mistakes:
- Forgetting brackets around declarations
- Confusing imports with declarations
- Thinking bootstrap cannot be an array
5. You want to add a new component
HeaderComponent to your Angular app. Which changes must you make in AppModule to include it properly?hard
Solution
Step 1: Import the new component
You must import HeaderComponent from its file to use it in AppModule.Step 2: Declare the component in declarations array
Adding HeaderComponent to declarations lets Angular recognize and use it in templates.Final Answer:
Add HeaderComponent to declarations array and import its file -> Option AQuick Check:
New components require import and declaration [OK]
Hint: Import and declare new components in AppModule [OK]
Common Mistakes:
- Adding component to imports instead of declarations
- Only adding to bootstrap instead of declarations
- Assuming no changes needed for new components
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
