Skip to content

Add delete function in the TaskService

  • βœ”οΈ
    Handle task deletion
    Learn how to delete a task from the task list.

The deleteTask function

To delete a task from the list, let’s create a deleteTask function in the TaskService service. It’ll remove the task from the list based on its uuid.

IπŸŽ“ nstructions

  1. Update the src/app/task.service.ts file.

    import { Injectable } from '@angular/core';
    @Injectable({
    providedIn: 'root'
    })
    export class TaskService {
    tasks: Task[] = [
    {
    title: 'Task 1',
    description: 'Description of task 1',
    createdAt: new Date()
    },
    {
    title: 'Task 2',
    description: 'Description of task 2',
    createdAt: new Date()
    }
    ];
    deleteTask(id: string) {
    this.tasks = this.tasks.filter(task => task.id !== id);
    }
    }

βœ”οΈ What you learned

In this chapter, you added a function to remove a task from the list based on its id in the TaskService.