How to pass route parameters in Angular

Route parameters in Angular enable passing dynamic data through URLs, allowing components to receive context-specific information for rendering appropriate content. As the creator of CoreUI with over 25 years of development experience, I’ve implemented parameterized routing in countless enterprise Angular applications. The most effective approach is defining route parameters in the route configuration and accessing them through ActivatedRoute in components. This provides clean, SEO-friendly URLs while maintaining proper data flow and component reusability across different contexts.

Define route parameters in route configuration and access them using ActivatedRoute service for dynamic component behavior based on URL data.

// app-routing.module.ts
import { NgModule } from '@angular/core'
import { RouterModule, Routes } from '@angular/router'

const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'user/:id/edit', component: UserEditComponent },
  { path: 'category/:categoryId/product/:productId', component: ProductComponent },
  { path: 'search/:term', component: SearchResultsComponent }
]

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

// user-detail.component.ts
import { Component, OnInit } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'

@Component({
  selector: 'app-user-detail',
  template: `
    <div *ngIf="userId">
      <h2>User Details for ID: {{ userId }}</h2>
      <button (click)="editUser()">Edit User</button>
    </div>
  `
})
export class UserDetailComponent implements OnInit {
  userId: string | null = null

  constructor(
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    // Get route parameter
    this.userId = this.route.snapshot.paramMap.get('id')

    // Observable approach for dynamic parameter changes
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id')
      // Fetch user data based on ID
    })
  }

  editUser() {
    // Navigate with parameter
    this.router.navigate(['/user', this.userId, 'edit'])
  }
}

// Navigation with parameters
function navigateToUser(userId: number) {
  this.router.navigate(['/user', userId])
}

Route parameters are defined using colon syntax (:id) in route paths and accessed via ActivatedRoute.paramMap. The snapshot.paramMap.get() method provides immediate parameter values, while paramMap.subscribe() handles dynamic parameter changes. Use router.navigate() with parameter arrays for programmatic navigation with dynamic values.

Best Practice Note:

This parameterized routing pattern is used extensively in CoreUI’s Angular admin templates for entity detail views and CRUD operations. Always validate route parameters and handle missing or invalid values gracefully to prevent application errors and improve user experience.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to change opacity on hover in CSS
How to change opacity on hover in CSS

How to get element ID in JavaScript
How to get element ID in JavaScript

How to Open Link in a New Tab in HTML?
How to Open Link in a New Tab in HTML?

How to round a number to two decimal places in JavaScript
How to round a number to two decimal places in JavaScript

Answers by CoreUI Core Team