Defining the Links
Build a link with an id in it by giving routerLink a list of parts: [routerLink]="['/categories', category.categoryId]" makes /categories/3. The same list works with Router's navigate, and the router joins and escapes the parts for you.
Journeyman · Angular · Updated
After this lesson you can
- Build links to a route with a parameter with [routerLink]
Key takeaways
- For an address with an id in it, give
[routerLink]a list of parts:['/categories', category.categoryId]makes/categories/3. - The router joins the parts with slashes and escapes each one, so a value with a space or a slash can't break the address.
Router'snavigatetakes the same list, such asnavigate(['/categories', added.categoryId]).
The last step: the list of categories links each one to its own page, with its id in the address.
A list of parts
Give [routerLink] (in square brackets, because it's now an expression) a list: the fixed part of the address, then the values to add.
import { Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { RouterLink } from '@angular/router';
import { Category, CategoryApi } from './category-api';
@Component({
imports: [RouterLink],
selector: 'app-categories-page',
template: `
<h1>Categories</h1>
<div class="list-group">
@for (category of categories(); track category.categoryId) {
<a class="list-group-item list-group-item-action" [routerLink]="['/categories', category.categoryId]">
{{ category.categoryName }}
</a>
}
</div>
`,
})
export class CategoriesPage {
protected readonly categories = toSignal(inject(CategoryApi).getAll(), { initialValue: [] as Category[] });
}
For Confections, ['/categories', category.categoryId] is ['/categories', 3], and the router joins the parts with slashes: /categories/3, which matches categories/:categoryId. Each link in the list gets its own address:
<a href="/categories/1">Beverages</a>
<a href="/categories/2">Condiments</a>
<a href="/categories/3">Confections</a>
Why a list, and not one string? The router escapes each part for you, so a value with a space or a slash in it can't break the address. And a list is easy to build from values.
You may also see the address built in the template, routerLink="/categories/{{ category.categoryId }}". It makes the same link for a number, but the list is the safer habit.
Links on the details page
The details page can link to its neighbors. categoryId() is the page's input from the last lesson:
<a [routerLink]="['/categories', categoryId() + 1]">Next category</a>
This is exactly the case where Angular reuses the page and only changes the input, and why the last lesson loaded the category in an effect.
In code
Router's navigate takes the same list. After adding a category, go to its new page:
this.api.add(category).subscribe(added => this.router.navigate(['/categories', added.categoryId]));
Try it
With the API running, open http://localhost:4200/categories, and click Seafood: the address is /categories/8, and the page shows Seafood. Use the browser's Back button, and you're on the list again.
Check yourself
1 question to try on the lesson page.
So what
[routerLink]="['/categories', id]" builds each item's address from its id, and navigate takes the same list. Route, input, links: that's a page for every item in your database.