Angular / Angular Routing and Navigation
Passing and Retrieving Route Parameters
This tutorial will help you understand how to pass and retrieve parameters between routes in Angular. You'll learn how to pass parameters in the URL and retrieve them using Activa…
Section overview
5 resourcesExplains configuring routes and enabling navigation in Angular applications.
1. Introduction
This tutorial aims to guide you through the process of passing and retrieving route parameters in Angular. By the end of this tutorial, you will be able to pass parameters between routes and retrieve them using ActivatedRoute.
You will learn:
- How to pass parameters in the URL
- How to retrieve these parameters using ActivatedRoute
Prerequisites:
- Basic understanding of Angular
- Familiarity with TypeScript
2. Step-by-Step Guide
In Angular, we can pass parameters through the URL and retrieve them in the component using ActivatedRoute. This is extremely useful when we want to pass data between routes.
First, we need to define our routes and specify where we want to pass the parameters.
Defining Routes
In the app-routing.module.ts file, we define our routes like this:
const routes: Routes = [
{ path: 'product/:id', component: ProductDetailComponent },
];
In this example, :id is a placeholder for the parameter we want to pass.
Passing Parameters
When navigating to a route, we can pass the parameter like this:
this.router.navigate(['/product', productId]);
In this example, productId is the parameter we want to pass to the ProductDetailComponent.
Retrieving Parameters
In the ProductDetailComponent, we can retrieve the id parameter using ActivatedRoute like this:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.paramMap.subscribe(params => {
let id = +params.get('id');
});
}
In this example, we subscribe to the paramMap observable from ActivatedRoute to get the id parameter.
3. Code Examples
Here are some practical examples.
Example 1: Passing and retrieving a single parameter
In this example, we're passing a single parameter (id) to the ProductDetailComponent.
app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ProductDetailComponent } from './product-detail/product-detail.component';
const routes: Routes = [
{ path: 'product/:id', component: ProductDetailComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
product-list.component.ts:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-product-list',
templateUrl: './product-list.component.html',
styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit {
products = [
{ id: 1, name: 'Product 1' },
{ id: 2, name: 'Product 2' },
// ...
];
constructor(private router: Router) {}
ngOnInit() {}
goToProductDetail(productId) {
this.router.navigate(['/product', productId]);
}
}
product-detail.component.ts:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-product-detail',
templateUrl: './product-detail.component.html',
styleUrls: ['./product-detail.component.css']
})
export class ProductDetailComponent implements OnInit {
id: number;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.paramMap.subscribe(params => {
this.id = +params.get('id');
});
}
}
In this example, when a user clicks on a product in the ProductListComponent, the goToProductDetail method is called and the id of the product is passed to the ProductDetailComponent through the URL.
In the ProductDetailComponent, the id is retrieved from the URL in the ngOnInit method.
4. Summary
In this tutorial, you learned how to pass and retrieve parameters between routes in Angular using the URL and ActivatedRoute.
Next steps for learning:
- Learn more about Angular Routing
5. Practice Exercises
-
Create a new Angular application and implement routing. Create two components and pass a parameter from one to the other.
-
Create a blog application with a list of posts and a detail page for each post. Pass the
idof the post from the list to the detail page. -
In the blog application, implement a feature to navigate to the next and previous post. Pass the
idof the next and previous post to the detail page.
Solutions and explanations for these exercises can be found on the Angular website.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article