Todo List (Standalone Component with @for)
@for and an explicit track expression[(ngModel)] for simple form input binding@for (...; track ...) control flow.Requirements
Use a standalone Angular component as the root component. Display a text input and an "Add" button for creating new todos. Render todo items with @forandtrack todo.id, so each row keeps its identity.Each todo item should show a label and a checkbox to toggle completion. Allow removing a single todo item from the list. Optionally provide a "Clear completed" action to remove all completed todos.
What the checks look for
The checks find elements by listitem, textbox, button "Add", checkbox, button "Remove". Keep these in your markup.
Expected behavior
Typing a non-empty value and clicking "Add" appends a new todo to the list and clears the input. Empty or whitespace-only values are ignored (no item is added). Clicking a todo’s checkbox toggles its completed state. Completed todos are visually distinguished (e.g. line-through). Clicking a remove button deletes that specific todo. If you implement "Clear completed", it removes only the completed todos.
Implementation notes
Keep everything in AppComponent(src/app/app.component.tsandapp.component.html). No extra services or components are required.Store the todos as an array of { id, text, done }objects. Holding the array in a signal and replacing it on every change (todos.update(...)) keeps anything computed from it correct.Use @forwithtrack todo.idso item identity stays stable without a separate tracking function.Use [(ngModel)]for the input binding (remember to importFormsModule); it binds to a signal as well as to a plain field.Use click handlers like (click)="addTask()"and(click)="removeTask(todo)"to change the list.
Solution idea
This exercise models a classic todo list in a single standalone component. The input text lives in a newTask signal bound with [(ngModel)], and the list lives in a todos signal holding { id, text, done } objects. hasTodos is computed from the list and switches between the list and the empty state. The template renders @for (todo of todos(); track todo.id), and every handler replaces the array through todos.update(...) instead of editing it in place.
Solution
Overview
This exercise models a classic todo list in a single standalone component. The input text lives in a newTask signal bound with [(ngModel)], and the list lives in a todos signal holding { id, text, done } objects. hasTodos is computed from the list and switches between the list and the empty state. The template renders @for (todo of todos(); track todo.id), and every handler replaces the array through todos.update(...) instead of editing it in place.
Typical flow:
- User types into the input;
[(ngModel)]="newTask"writes each change into the signal. - Clicking Add calls
addTask(), which trims the text, ignores empties, appends a todo with the next id, and clearsnewTask. - Each row's checkbox shows
todo.donethrough[checked]and callstoggleTask(todo.id)on change, which swaps in a copy of that todo withdoneflipped; the label gets the.doneclass. - Clicking the remove button calls
removeTask(todo.id), which filters that item out. - "Clear completed" calls
clearCompleted(), which keeps only the todos that are not done.
Test cases
- With an empty input, clicking Add does nothing.
- Typing "Buy milk" and clicking Add shows a single todo row with that text.
- Toggling the checkbox marks the item as completed and applies the completed styling.
- Clicking the remove button deletes that specific row.
- After completing multiple todos, clicking "Clear completed" removes only the completed ones.
Signal state with immutable list updates
Keep the input text and the list in signals: newTask = signal('') and todos = signal<Todo[]>([]). A plain nextId counter supplies ids; the template never reads it, so it does not need to be a signal. hasTodos = computed(() => this.todos().length > 0) drives the @if around the list and the footer.
addTask() reads newTask(), trims it, returns early when it is empty, then appends with todos.update((todos) => [...todos, { id: this.nextId++, text, done: false }]) and resets the input with newTask.set(''). toggleTask(id) maps over the list and replaces only the matching todo with { ...todo, done: !todo.done }; removeTask(id) and clearCompleted() filter.
Every change produces a new array, and a toggled todo is a new object. That is what lets computed values and OnPush change detection see the change: a signal compares values with Object.is, so pushing into the same array or flipping todo.done in place leaves the signal unchanged. For the same reason the checkbox uses [checked]="todo.done" with (change)="toggleTask(todo.id)" instead of [(ngModel)]="todo.done", which writes into the object directly. The text input keeps [(ngModel)], which binds two-way to a signal.
Files
{
"name": "angular-todo-list-solution",
"private": true
}
{
"compilerOptions": {
"target": "es2022",
"strict": true,
"noEmitOnError": false
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Angular SDK • Todo List (solution)</title>
</head>
<body>
<app-root></app-root>
</body>
</html>
import 'zone.js';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent).catch(console.error);
:root {
color-scheme: dark;
}
html, body {
margin: 0;
height: 100%;
background: #0a0a0a;
}
body {
font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}
.panel {
background: #0b1020;
border: 1px solid #1f2937;
border-radius: 12px;
padding: 16px;
display: grid;
gap: 12px;
}
.input-row {
display: flex;
gap: 8px;
}
.input-row input {
flex: 1;
background: #020617;
border-radius: 8px;
border: 1px solid #1f2937;
padding: 8px 10px;
color: #e5e7eb;
font-size: 14px;
}
.input-row input:focus {
outline: none;
border-color: #6366f1;
box-shadow: 0 0 0 1px #6366f1;
}
.primary {
background: #4ade80;
color: #022c22;
border: none;
border-radius: 999px;
padding: 8px 14px;
font-weight: 600;
font-size: 14px;
cursor: pointer;
}
.primary[disabled] {
opacity: .6;
cursor: not-allowed;
}
.list {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 6px;
}
.item {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: 6px 8px;
border-radius: 8px;
background: #020617;
border: 1px solid transparent;
}
.item-main {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
}
.item-main input[type=checkbox] {
accent-color: #22c55e;
}
.item-main span.done {
text-decoration: line-through;
color: #9ca3af;
}
button.ghost {
background: transparent;
color: #e5e7eb;
border: 1px solid #374151;
border-radius: 999px;
padding: 6px 10px;
font-size: 12px;
cursor: pointer;
}
button.ghost.small {
padding: 4px 10px;
font-size: 12px;
}
.empty {
margin: 4px 0 0;
font-size: 13px;
color: #9ca3af;
text-align: center;
}
.footer {
display: flex;
justify-content: flex-end;
margin-top: 4px;
}
<main style="min-height:100vh; background:#0a0a0a; color:#e5e7eb; display:grid; place-items:center; padding:24px;">
<section style="width:100%; max-width:480px; display:grid; gap:16px;">
<h1 style="margin:0; font-size:20px; font-weight:700;">Todo List</h1>
<div class="panel">
<div class="input-row">
<input type="text" placeholder="Add a new task..." [(ngModel)]="newTask" />
<button type="button" class="primary" (click)="addTask()">Add</button>
</div>
@if (hasTodos()) {
<ul class="list">
@for (todo of todos(); track todo.id) {
<li class="item">
<label class="item-main">
<input type="checkbox" [checked]="todo.done" (change)="toggleTask(todo.id)" />
<span [class.done]="todo.done">{{ todo.text }}</span>
</label>
<button type="button" class="ghost" (click)="removeTask(todo.id)">Remove</button>
</li>
}
</ul>
} @else {
<p class="empty">No tasks yet. Add your first one above.</p>
}
@if (hasTodos()) {
<div class="footer">
<button type="button" class="ghost small" (click)="clearCompleted()">Clear completed</button>
</div>
}
</div>
</section>
</main>
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
interface Todo {
id: number;
text: string;
done: boolean;
}
@Component({
selector: 'app-root',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [FormsModule],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
readonly newTask = signal('');
readonly todos = signal<Todo[]>([]);
readonly hasTodos = computed(() => this.todos().length > 0);
private nextId = 1;
addTask(): void {
const text = this.newTask().trim();
if (!text) {
return;
}
this.todos.update((todos) => [...todos, { id: this.nextId++, text, done: false }]);
this.newTask.set('');
}
toggleTask(id: number): void {
// Replace the changed todo instead of editing it, so everything derived from todos() sees the change.
this.todos.update((todos) =>
todos.map((todo) => (todo.id === id ? { ...todo, done: !todo.done } : todo))
);
}
removeTask(id: number): void {
this.todos.update((todos) => todos.filter((todo) => todo.id !== id));
}
clearCompleted(): void {
this.todos.update((todos) => todos.filter((todo) => !todo.done));
}
}
Notes & Pitfalls
Allowing empty or whitespace-only todos to be added. Pushing into the array held by the signal or flipping todo.donein place: the signal keeps the same reference, so computed values andOnPushviews miss the change.Reusing one id for several items, which breaks the identity track todo.idrelies on.Forgetting to import FormsModule, which breaks[(ngModel)]bindings.
Holding list state in a signaland replacing it withupdateon every change.Deriving hasTodoswithcomputed.Binding [(ngModel)]two-way to a signal for the text input.Rendering lists with @forandtrack todo.idfor stable identity.
Common mistakes on this challenge
Fix the Angular template compilation contract first
Framework assertions cannot be trusted until the standalone component and its modern control-flow template compile and boot. Fix the first Angular compiler diagnostic, then check every @if,@for, or@switchblock for balanced braces and a validtrackexpression.Verify standalone importsincludes the forms modules, directives, or pipes the template actually uses; built-in control flow alone does not requireCommonModule.Wait for the preview to boot successfully before debugging assertion mismatches, then re-run one behavior check at a time.
Call a signal where the template reads it
The page shows a "[Signal: …]" label, which Angular prints when a template reads a signal without calling it. Write count()rather thancountin interpolations: a signal is a function, and the template needs its value.Check property bindings as well: [disabled]="isEmpty"binds the function, which is always truthy, while[disabled]="isEmpty()"binds the boolean.Inputs, models and computed values read the same way, for example rating(),value()orfilteredUsers()inside@for.
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.