Thuta Learning
Angular
AdvancedWeb DevelopmentIntermediate

Route Parameters

AngularLesson 3325 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Route Parameters
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Route Parameters, in plain language

You can access URL parameters to build dynamic routes.

Problem → Why → Solution

The problem — why this concept matters

Reading only a snapshot can leave stale data when the ID changes while the component is reused.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.

Mental model
  1. URL or navigation
  2. router config and guard
  3. component view

Explain the core concept and Angular data flow of Route Parameters

Core syntax or pattern

typescript
// app-routing.module.ts
const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'product/:id/:name', component: ProductComponent }
];

// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class UserDetailComponent implements OnInit {
  userId: string;
  
  constructor(private route: ActivatedRoute) {}
  
  ngOnInit() {
    // Method 1: Snapshot (one-time read)
    this.userId = this.route.snapshot.paramMap.get('id');
    
    // Method 2: Observable (reactive, updates when params change)
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id');
      this.loadUser(this.userId);
    });
  }
}

// Navigate programmatically
constructor(private router: Router) {}

goToUser(id: number) {
  this.router.navigate(['/user', id]);
}

Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.

Read the code step by step

  1. URL or navigation// app-routing.module.ts

    You can access URL parameters to build dynamic routes.

  2. router config and guardthis.userId = this.route.snapshot.paramMap.get('id');

    Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.

  3. component view}

    Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.

Execution flow
  1. URL or navigation
  2. router config and guard
  3. component view

Success

Explain the core concept and Angular data flow of Route Parameters

Failure

Reading only a snapshot can leave stale data when the ID changes while the component is reused.

You can access URL parameters to build dynamic routes.

typescript
// app-routing.module.ts
const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'product/:id/:name', component: ProductComponent }
];

// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class UserDetailComponent implements OnInit {
  userId: string;
  
  constructor(private route: ActivatedRoute) {}
  
  ngOnInit() {
    // Method 1: Snapshot (one-time read)
    this.userId = this.route.snapshot.paramMap.get('id');
    
    // Method 2: Observable (reactive, updates when params change)
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id');
      this.loadUser(this.userId);
    });
  }
}

// Navigate programmatically
constructor(private router: Router) {}

goToUser(id: number) {
  this.router.navigate(['/user', id]);
}
You should see
(You can read parameters from the URL and use them inside the component)

Real-world usage

How to apply it in a real project

Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.

Mini challenge

Build it yourself

Rebuild the Route Parameters example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// app-routing.module.ts
const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'product/:id/:name', component: ProductComponent }
];

// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class UserDetailComponent implements OnInit {
  userId: string;
  
  constructor(private route: ActivatedRoute) {}
  
  ngOnInit() {
    // Method 1: Snapshot (one-time read)
    this.userId = this.route.snapshot.paramMap.get('id');
    
    // Method 2: Observable (reactive, updates when params change)
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id');
      this.loadUser(this.userId);
    });
  }
}

// Navigate programmatically
constructor(private router: Router) {}

goToUser(id: number) {
  this.router.navigate(['/user', id]);
}

Expected: Explain the core concept and Angular data flow of Route Parameters

Show hint

Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.

Quick quiz 1 / 3

What best describes the core idea of Route Parameters?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • URL or navigation → router config and guard → component view
  • Route parameters should represent resource identity, query parameters optional filters, sorting, or pagination, and fragments document locations. Because a component may be reused, subscribe to parameter changes or use route-input binding when a one-time snapshot is insufficient. Treat URL text as untrusted input and parse, validate, handle not-found, and cancel stale requests.
  • Reading only a snapshot can leave stale data when the ID changes while the component is reused.
  • Build products/:id with ID validation, switched requests on parameter change, a not-found state, and a query-synchronized tab.

Angular — Routing — Angular

Easy traps

  • Reading only a snapshot can leave stale data when the ID changes while the component is reused.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Route Parameters example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (You can read parameters from the URL and use them inside the component)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Route Parameters | Thuta Learning