Thuta Learning
Angular
ProjectsWeb DevelopmentIntermediate

Mini Project: Task Tracker (Part 2 - Adding the Core Feature)

AngularLesson 4530 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Mini Project: Task Tracker (Part 2 - Adding the Core Feature)
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

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.

Mental model
  1. project requirements
  2. components, state, and services
  3. 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

typescript
// 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

  1. project requirements// task.service.ts

    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.

  2. components, state, and servicesaddTask(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.

  3. 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.

Execution flow
  1. project requirements
  2. components, state, and services
  3. 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

typescript
// 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);
  }
}
You should see
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
typescript
// 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

What best describes the core idea of Mini Project: Task Tracker (Part 2 - Adding the Core Feature)?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Replacing source state with a filtered list can make tasks disappear when filters change.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

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.

You'll know it worked when: 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.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Mini Project: Task Tracker (Part 2 - Adding the Core Feature) | Thuta Learning