Explain it simply
Mini Project: Task Tracker (Part 2 - Adding the Core Feature), in plain language
Building on the structure from Part 1, in Part 2 we'll add the app's core feature: task add/list/toggle functionality. The services, dependency injection, reactive forms, *ngFor directive, and two-way binding lessons are all foundational for this stage. We'll centralize a single TaskService to share data between components, managing the task list with a BehaviorSubject. This keeps component logic simple and reusable.
Problem → Why → Solution
The problem — why this concept matters
Replacing source state with a filtered list can make tasks disappear when filters change.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
Part 2 implements add, toggle, delete, and filter as explicit state transitions. Keep form validation at the UI boundary while a state service exposes immutable updates and a readonly view model. Derive counts and filtered results rather than storing duplicate state that can drift out of sync.
- project requirements
- components, state, and services
- tested UI
Explain the core concept and Angular data flow of Mini Project: Task Tracker (Part 2 - Adding the Core Feature)
Core syntax or pattern
// task.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Task } from './task.model';
@Injectable({ providedIn: 'root' })
export class TaskService {
private tasksSubject = new BehaviorSubject<Task[]>([]);
tasks$ = this.tasksSubject.asObservable();
private nextId = 1;
getTasks() {
return this.tasks$;
}
addTask(title: string, description: string) {
const current = this.tasksSubject.value;
const newTask: Task = { id: this.nextId++, title, description, completed: false };
this.tasksSubject.next([...current, newTask]);
}
toggleComplete(id: number) {
const updated = this.tasksSubject.value.map(t =>
t.id === id ? { ...t, completed: !t.completed } : t
);
this.tasksSubject.next(updated);
}
}Part 2 implements add, toggle, delete, and filter as explicit state transitions. Keep form validation at the UI boundary while a state service exposes immutable updates and a readonly view model. Derive counts and filtered results rather than storing duplicate state that can drift out of sync.
Read the code step by step
- project requirements
// task.service.tsBuilding on the structure from Part 1, in Part 2 we'll add the app's core feature: task add/list/toggle functionality. The services, dependency injection, reactive forms, *ngFor directive, and two-way binding lessons are all foundational for this stage. We'll centralize a single TaskService to share data between components, managing the task list with a BehaviorSubject. This keeps component logic simple and reusable.
- components, state, and services
addTask(title: string, description: string) {Part 2 implements add, toggle, delete, and filter as explicit state transitions. Keep form validation at the UI boundary while a state service exposes immutable updates and a readonly view model. Derive counts and filtered results rather than storing duplicate state that can drift out of sync.
- tested UI
}Build the add form, task-row outputs, filters, and remaining count; test duplicate and empty titles, rapid toggles, and deletion of the selected detail task.
- project requirements
- components, state, and services
- tested UI
Success
Explain the core concept and Angular data flow of Mini Project: Task Tracker (Part 2 - Adding the Core Feature)
Failure
Replacing source state with a filtered list can make tasks disappear when filters change.
Let's think about this for a moment
Building on the structure from Part 1, in Part 2 we'll add the app's core feature: task add/list/toggle functionality. The services, dependency injection, reactive forms, *ngFor directive, and two-way binding lessons are all foundational for this stage. We'll centralize a single TaskService to share data between components, managing the task list with a BehaviorSubject. This keeps component logic simple and reusable.
Let's build it for real
Create `TaskService` with `ng generate service task`, and implement three methods — `getTasks()`, `addTask(task)`, `toggleComplete(id)` — around an internal BehaviorSubject<Task[]>. In TaskListComponent, build a FormGroup (title, description) for adding a new task, and call service.addTask() on submit. Loop over the task list with *ngFor, and wire up each checkbox with [(ngModel)] to toggle completed status. Give completed tasks a strikethrough style with ngClass.
Code Example
// task.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Task } from './task.model';
@Injectable({ providedIn: 'root' })
export class TaskService {
private tasksSubject = new BehaviorSubject<Task[]>([]);
tasks$ = this.tasksSubject.asObservable();
private nextId = 1;
getTasks() {
return this.tasks$;
}
addTask(title: string, description: string) {
const current = this.tasksSubject.value;
const newTask: Task = { id: this.nextId++, title, description, completed: false };
this.tasksSubject.next([...current, newTask]);
}
toggleComplete(id: number) {
const updated = this.tasksSubject.value.map(t =>
t.id === id ? { ...t, completed: !t.completed } : t
);
this.tasksSubject.next(updated);
}
}Typing a task's title/description into the form and submitting it instantly adds a new task to the list, and clicking the checkbox switches it to a strikethrough style.Real-world usage
How to apply it in a real project
Build the add form, task-row outputs, filters, and remaining count; test duplicate and empty titles, rapid toggles, and deletion of the selected detail task.
Mini challenge
Build it yourself
Rebuild the Mini Project: Task Tracker (Part 2 - Adding the Core Feature) example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// task.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Task } from './task.model';
@Injectable({ providedIn: 'root' })
export class TaskService {
private tasksSubject = new BehaviorSubject<Task[]>([]);
tasks$ = this.tasksSubject.asObservable();
private nextId = 1;
getTasks() {
return this.tasks$;
}
addTask(title: string, description: string) {
const current = this.tasksSubject.value;
const newTask: Task = { id: this.nextId++, title, description, completed: false };
this.tasksSubject.next([...current, newTask]);
}
toggleComplete(id: number) {
const updated = this.tasksSubject.value.map(t =>
t.id === id ? { ...t, completed: !t.completed } : t
);
this.tasksSubject.next(updated);
}
}Expected: Explain the core concept and Angular data flow of Mini Project: Task Tracker (Part 2 - Adding the Core Feature)
Show hint
Build the add form, task-row outputs, filters, and remaining count; test duplicate and empty titles, rapid toggles, and deletion of the selected detail task.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- project requirements → components, state, and services → tested UI
- Part 2 implements add, toggle, delete, and filter as explicit state transitions. Keep form validation at the UI boundary while a state service exposes immutable updates and a readonly view model. Derive counts and filtered results rather than storing duplicate state that can drift out of sync.
- Replacing source state with a filtered list can make tasks disappear when filters change.
- Build the add form, task-row outputs, filters, and remaining count; test duplicate and empty titles, rapid toggles, and deletion of the selected detail task.
Angular — Overview — Angular