Sub topic 5
Splitting out a task item with input() and output()
This chapter assumes you can read TypeScript, you have a standalone component that renders a task list, and you know the @for block and property and event binding. Here you move the markup for one task into its own component.
Why split the item out
In the list component, each row handles its own checkbox, title, and delete button. That works, but the list component now knows how every row looks. A child component for one task gives you three things:
- The row markup lives in one place.
- The child can be tested and reused without the list.
- The data flow is explicit: values go down, events come up.
The child does not change state itself. It receives a task and reports what the user did. The parent decides what to do about it. This kind of child is called a presentational component.
The child component
The child declares one input and two outputs.
import { Component, input, output } from '@angular/core';
import { Task } from './task';
@Component({
selector: 'app-task-item',
template: `
<input type="checkbox" [checked]="task().done" (change)="toggled.emit(task().id)" />
<span>{{ task().title }}</span>
<button type="button" (click)="removed.emit(task().id)">Delete</button>
`,
})
export class TaskItemComponent {
readonly task = input.required<Task>();
readonly toggled = output<number>();
readonly removed = output<number>();
}input.required<Task>() creates a signal input. Its value is a signal, so you read it by calling it: task(). The required form makes the compiler report an error if a parent uses <app-task-item> without passing task. Without required, you would write input<Task>(), and the value would be undefined until set.
A signal input is read-only inside the child. You cannot assign to this.task. Data flows one way, from parent to child.
output<number>() creates an output. Calling .emit(value) sends the value to whoever listens. Here the child emits the task id, so the parent knows which task the user acted on.
Using the child in the parent
The parent binds to the input with square brackets and listens to outputs with parentheses. Inside an output handler, $event holds the emitted value.
import { Component, inject } from '@angular/core';
import { TaskService } from './task.service';
import { TaskItemComponent } from './task-item.component';
@Component({
selector: 'app-task-list',
imports: [TaskItemComponent],
template: `
<ul>
@for (task of tasks(); track task.id) {
<li>
<app-task-item
[task]="task"
(toggled)="toggle($event)"
(removed)="remove($event)"
/>
</li>
} @empty {
<li>No tasks yet.</li>
}
</ul>
<p>{{ remaining() }} remaining</p>
`,
})
export class TaskListComponent {
private readonly service = inject(TaskService);
readonly tasks = this.service.tasks;
readonly remaining = this.service.remaining;
toggle(id: number): void {
this.service.toggle(id);
}
remove(id: number): void {
this.service.remove(id);
}
}Two details matter here. The child must be listed in the parent's imports array, or Angular will not recognize <app-task-item>. And [task]="task" passes the loop variable, not a signal. Angular writes it into the child's signal input for you.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Error that `task` is required | The parent omitted `[task]` | Add the binding, or use `input<Task>()` if it is optional |
| `'app-task-item' is not a known element` | The child is missing from `imports` | Add `TaskItemComponent` to the parent's `imports` |
| Template shows `[object Object]` or a type error | You wrote `task.title` instead of `task().title` in the child | Call the input to read it |
| Handler receives an event object | You used `$event` with a native DOM event, not an output | Emit the value you need with `.emit()` |
Exercise
Exercise
In TaskItemComponent, what happens if you replace input.required<Task>() with input<Task>(), and why would the template task().title then cause a problem?
Show answer
The input becomes optional, so its type is Task | undefined and its value is undefined until a parent sets it. Under strict template checking, task().title fails to compile because task() may be undefined. You would need a guard such as @if (task(); as t) or the optional chaining operator task()?.title. input.required avoids this because the compiler forces every parent to pass a value.
Exercise
Add a second output to TaskItemComponent so the user can rename a task. What type should it emit, and how does the parent handle it?
Show answer
Declare readonly renamed = output<{ id: number; title: string }>(); and emit { id: task().id, title: newTitle } from the child. In the parent, add (renamed)="rename($event)" and a rename method that calls a new TaskService.rename method, which updates the signal immutably with map. The child still never changes state itself.