Sub topic 2
Splitting a Task Item with `input()` and `output()`
Assume you know JavaScript arrays and objects, basic TypeScript interfaces, and Angular standalone components. This chapter separates one task row from its parent, then connects it to shared signal state and a validated form.
Passing data and events
A child component receives data with an input and reports events with an output. input.required<Task>() means the parent must provide a Task. output<number>() creates an event carrying a task ID.
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>();
}The parent binds [task]="task" and listens with (toggled) and (removed). The child does not change application state directly.
flowchart LR
P[Task list parent] -->|task input| C[Task item]
C -->|toggled or removed output| P
P --> S[Task service]Holding state in signals
A signal is a reactive value that notifies Angular when it changes. Keep the writable signal private, and expose a read-only signal to components.
private readonly _tasks = signal<Task[]>([]);
readonly tasks = this._tasks.asReadonly();
readonly remaining = computed(() =>
this._tasks().filter(task => !task.done).length
);Update arrays immutably. This creates a new array instead of modifying the existing one.
Sharing state through a service
An injectable service is a class Angular can create and share through dependency injection. A root-provided service gives the application one task store.
@Injectable({ providedIn: 'root' })
export class TaskService {
private readonly _tasks = signal<Task[]>([]);
readonly tasks = this._tasks.asReadonly();
toggle(id: number): void {
this._tasks.update(tasks =>
tasks.map(task =>
task.id === id ? { ...task, done: !task.done } : task
)
);
}
}The parent injects TaskService, passes each task to TaskItemComponent, and calls toggle or remove when it receives an event.
Adding tasks with validation
A reactive form keeps form state in a FormControl. Validators define acceptable input before submission.
readonly title = new FormControl('', {
nonNullable: true,
validators: [Validators.required, Validators.maxLength(100)],
});
submit(): void {
const value = this.title.value.trim();
if (this.title.valid && value) {
this.service.add(value);
this.title.reset();
}
}Disable the submit button while title.invalid is true. Still validate inside submit(), because event handlers should not rely only on the template.
Exercise
Why should the child emit a task ID instead of toggling the task itself?
Show answer
The parent or service owns application state, so the child remains presentational and easier to reuse and test.