Sub topic 9
Adding a task detail route
This chapter adds a second page to the app. The list view stays at /, and a new detail view lives at /tasks/:id. It assumes you have the TaskService from the earlier chapters, standalone components, and signals. It also assumes you know basic URL structure.
How the router fits together
The router watches the browser URL and matches it against a list of routes. The matching route's component is rendered in a <router-outlet>, a placeholder element in the root template.
Defining routes and registering the router
A route maps a path to a component. A segment that starts with a colon, such as :id, is a route parameter that matches any value.
// app.routes.ts
import { Routes } from '@angular/router';
import { TaskListComponent } from './task-list.component';
import { TaskDetailComponent } from './task-detail.component';
export const routes: Routes = [
{ path: '', component: TaskListComponent },
{ path: 'tasks/:id', component: TaskDetailComponent },
{ path: '**', redirectTo: '' },
];
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes, withComponentInputBinding()), provideHttpClient()],
};The router checks routes from top to bottom and uses the first match. The ** wildcard matches everything, so it must come last. Here it sends unknown URLs back to the list.
withComponentInputBinding() is the feature that passes route parameters into component inputs. Without it, an input named id stays unset.
Giving the root component an outlet
The router needs somewhere to render. Put a <router-outlet /> in the root component:
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
template: `
<h1>Tasks</h1>
<router-outlet />
`,
})
export class App {}Building the detail component
The route parameter arrives as an input named after the parameter. Route parameters are always strings, so convert the value before comparing it with Task.id, which is a number. A computed signal keeps the lookup reactive.
import { Component, computed, inject, input } from '@angular/core';
import { RouterLink } from '@angular/router';
import { TaskService } from './task.service';
@Component({
selector: 'app-task-detail',
imports: [RouterLink],
template: `
@if (task(); as current) {
<h2>{{ current.title }}</h2>
<p>{{ current.done ? 'Done' : 'Not done yet' }}</p>
} @else {
<p>Task not found.</p>
}
<a routerLink="/">Back to list</a>
`,
})
export class TaskDetailComponent {
private readonly service = inject(TaskService);
readonly id = input.required<string>();
readonly task = computed(() =>
this.service.tasks().find(t => t.id === Number(this.id()))
);
}If the task is removed while the page is open, task() becomes undefined and the template shows the fallback message. The @else branch also covers URLs like /tasks/abc, because Number('abc') is NaN and matches nothing.
Linking from the list
Use routerLink instead of a plain href. A plain href reloads the whole page and discards the signal state in TaskService. routerLink navigates inside the running app. Import RouterLink in TaskListComponent and change the title span:
// in TaskListComponent's decorator
imports: [RouterLink],
// in its template, replacing the <span>
// <a [routerLink]="['/tasks', task.id]">{{ task.title }}</a>The array form builds the URL /tasks/2 and encodes each segment safely.
Common errors
- The
idinput is never set. You forgotwithComponentInputBinding(), or the input name differs from the route parameter name. - Task never found. You compared a string to a number. Convert with
Number(). - Nothing renders. The root template has no
<router-outlet />, orRouterOutletis missing fromimports. - State resets on navigation. You used
hrefand caused a full page reload.
Exercise
A user opens /tasks/99, and no task with id 99 exists. What does the detail page show, and why does it not throw an error?
Show answer
It shows "Task not found." The computed calls find, which returns undefined when nothing matches, and the @if block is false for undefined, so the @else branch renders.
Exercise
Why does TaskDetailComponent declare id as input.required<string>() rather than input.required<number>()?
Show answer
Route parameters come from the URL, so the router passes them as strings. The type must match what actually arrives, and the component converts it with Number() when it needs a number.