
- Angular Tutorial
- Angular - Home
- Angular - Overview
- Angular - Features
- Angular - Advantages & Disadvantages
- Angular Basics
- Angular - Environment setup
- Angular - First Application
- Angular - MVC Architecture
- Angular Components
- Angular - Components
- Angular - Component Lifecycle
- Angular - View Encapsulation
- Angular - Component Interaction
- Angular - Component Styles
- Angular - Nested Components
- Angular - Content projection
- Angular - Dynamic components
- Angular - Elements
- Angular Templates
- Angular - Templates
- Angular - Template statements
- Angular - Template Variables
- Angular - SVG as Templates
- Angular Binding
- Angular - Data Binding
- Angular - Interpolation
- Angular - Event Binding
- Angular - Property Binding
- Angular - Attribute Binding
- Angular - Class Binding
- Angular - Style Binding
- Angular - Two-way Binding
- Angular Directives
- Angular - Directives
- Angular - Attribute Directives
- Angular - Structural Directives
- Angular - Custom Directives
- Angular Pipes
- Angular - Pipes
- Angular - Built-in Pipes
- Angular - Custom Pipes
- Angular Forms
- Angular - Forms
- Angular - Template Driven Forms
- Angular - Reactive Forms
- Angular - Form Validation
- Angular - Dynamic Forms
- Angular Dependency Injection
- Angular - Dependency Injection
- Angular - Injectable Service
- Angular Routing
- Angular - Routing
- Angular - Dynamic Routes
- Angular - Wildcard Routes
- Angular - Nested Routes
- Angular - Navigation
- Angular - Routing in SPA
- Angular - Custom Route Matches
- Angular - Router Reference
- Angular HTTP Client programming
- Angular - Services
- Angular - HTTP Client
- Angular - Request
- Angular - Response
- Angular - GET
- Angular - POST
- Angular - PUT
- Angular - DELETE
- Angular - JSONP
- Angular - CRUD Operations Using HTTP
- Angular Modules
- Angular - Introduction to Modules
- Angular - Root Module
- Angular - Feature Module
- Angular - Sharing Module
- Angular - Routing Module
- Angular - NgModules
- Angular Animation
- Angular - Animations
- Angular Service Workers & PWA
- Angular - Service Workers & PWA
- Angular Testing
- Angular - Testing Overview
- Angular Design Patterns
- Angular - Design Patterns
- Angular - Lazy Loading
- Angular - Singleton Pattern
- Angular - Observer Pattern
- Angular Libraries
- Angular - Libraries
- Angular - Angular Material
- Angular - PrimeNG
- Angular - RxJS
- Angular Advanced
- Angular - Signals
- Angular - Authentication & Authorization
- Angular - Internationalization
- Angular - Standalone Component
- Angular - Accessibility
- Angular - Web Workers
- Angular - Server Side Rendering
- Angular - Ivy Compiler
- Angular - Building with Bazel
- Angular - Backward Compatibility
- Angular - Reactive Programming
- Angular Tools
- Angular - CLI
- Angular Material UI Elements
- Angular - Paginator
- Angular - Datepicker
- Angular - Select Drop-down
- Angular Miscellaneous
- Angular - Third Party Controls
- Angular - Configuration
- Angular - Displaying Data
- Angular - Decorators & Metadata
- Angular - Basic Example
- Angular - Error Handling
- Angular - Testing & Building a Project
- Angular - Lifecycle Hooks
- Angular - User Input
- Angular - What's New?
- Angular Useful Resources
- Angular - Quick Guide
- Angular - Useful Resources
- Angular - Discussion
Angular - Nested Routes
This chapter will discuss, what is Nested Routes and Nested Routing in Angular, including its syntax, usage and example of using in real-time angular project.
Nested Routes in Angular
In Angular, nested routes are used to define hierarchical routes for components. This technique allows you to load a specific child component only when its parent component is loaded.
For example, if you want to load all the items within the ItemsComponent, you can use a route like /items/item-lists/, where the "item-list" is nested within the "items routes".
When the <router-outlet> directive is used in a component other than the root component, it allows you to display child routes within that component, and those routes will be considered child routes of the parent component. This concept is known as nested routing, and it can be achieved by implementing nested routes.
Syntax of Angular Nested Routes
Below is the syntax to create Nested Routes in Angular −
const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ] } ];
Here,
- path: It specifies the URL segment for the route.
- component: It defines the component to be rendered for the route.
- children: An array of child routes nested under the parent route.
Note: The nested URL will be: "/parent/child1" and "/parent/child2".
Example of Angular Nested Routes
Below is a example of using the Nested Routes in a Angular project −
Step 1: Define Nested Routes for your application
import { Routes } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { ViewItemComponent } from './view-item/view-item.component'; import { PagenotfoundComponent } from './pagenotfound/pagenotfound.component'; import { EditItemComponent } from './edit-item/edit-item.component'; export const routes: Routes = [ {path: 'home', component: HomeComponent}, {path: 'about', component: AboutComponent}, {path: 'item', children:[ {path: 'view/:id', component: ViewItemComponent}, {path: 'edit/:id', component: EditItemComponent}, ]}, {path: '**', component: PagenotfoundComponent} ];
Step 2: Add your routes to your application
<h1>Angular Routing</h1> <a routerLink="/home">Home</a><br> <a routerLink="/about">About</a> Item1: <a routerLink="/item/view/1">View Item1</a><a routerLink="item/edit/1">Edit Item1</a><br> Item2: <a routerLink="item/view/2">View Item2</a><a routerLink="item/edit/2">Edit Item2</a><br> <router-outlet></router-outlet>
Step 2: Add the 'router-outlet' directive to your ItemComponent
<p>item works!>/p> <router-outlet></router-outlet>
Step 3: Access 'id' parameter in ViewItemComponent and EditItemComponent
import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-view-item', imports: [], templateUrl: './view-item.component.html', styleUrl: './view-item.component.css' }) export class ViewItemComponent implements OnInit{ id: any; constructor(private route: ActivatedRoute){} ngOnInit(): void { this.route.paramMap.subscribe(res=>{ this.id = res.get('id'); }); } }
EditItemComponent:
import { Component } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-edit-item', imports: [], templateUrl: './edit-item.component.html', styleUrl: './edit-item.component.css' }) export class EditItemComponent { id: any; constructor(private route: ActivatedRoute){} ngOnInit(): void { this.route.paramMap.subscribe(res=>{ this.id = res.get('id'); }); } }
Now run the application and see the output:

By observing the URL in the above GIF, you can clearly see that the View and Edit components are only loaded when the Item route is activated. In the context of Angular, the child components (View and Edit) are rendered only within the parent Item route.