AI Hints
Home›Angular Tutorial›Route Parameters, Navigating in Code and Redirects and Not Found
Angular · Lesson 77 of 120

Navigating in Code

The Router service.

⏱ 2 min read▶ Video at 1:32:25🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Sometimes code must change the page. After saving a form, or when the user clicks next. The router service does that.

  • After saving
  • Or clicking next
  • The Router service

Router.navigate

Inject the router. Navigate takes an array, just like router link. Navigate by URL takes a whole address, as text. Both change the page, without reloading.

keyvalue
router = inject(Router)get the router
navigate(['/books', 4])an array of parts
navigateByUrl('/books/4')a whole address
No reloadjust like routerLink

Next book

The component injects the router. Next navigates to the id plus one. All books goes back to the list. We start at book one, and click next twice. Run it. We're on book three, Circe. The same component stayed on screen, and its id input simply changed.

book-detail.tstypescript
import { Component, computed, inject, input } from '@angular/core';
import { Router } from '@angular/router';
import { BOOKS } from './books';

@Component({
  selector: 'app-book-detail',
  template: `
    <h2>{{ book()?.title }}</h2>
    <button (click)="next()">Next book</button>
    <button (click)="router.navigate(['/books'])">All books</button>
  `,
})
export class BookDetail {
  router = inject(Router);
  id = input.required<string>();
  book = computed(() => BOOKS.find(b => b.id === Number(this.id())));

  next() { this.router.navigate(['/books', Number(this.id()) + 1]); }
}
Also uses book-list.ts
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { BOOKS } from './books';

@Component({
  selector: 'app-book-list',
  imports: [RouterLink],
  template: `
    <h2>Books</h2>
    @for (b of books; track b.id) {
      <p><a [routerLink]="['/books', b.id]">{{ b.title }}</a></p>
    }
  `,
})
export class BookList {
  books = BOOKS;
}
Also uses app.routes.ts
import { Routes } from '@angular/router';
import { BookList } from './book-list';
import { BookDetail } from './book-detail';

export const routes: Routes = [
  { path: 'books', component: BookList },
  { path: 'books/:id', component: BookDetail },
];
Also uses app.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet],
  template: `<h1>Page Turners</h1><router-outlet />`,
})
export class App {}
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];
Also uses actions.json
[{"click": "button", "times": 2}]
Also uses _url
/books/1
Also uses _bar
1

Summary

Inject the router, and call navigate, or navigate by URL, to change the page from code. Next, redirects, and a not found page.

Key takeaways
  • inject(Router)
  • navigate([...])
  • navigateByUrl()
Next up: Redirects and Not Found — handle every address