Delete task with the HTTP client
- βοΈDelete a task with the HTTP clientLearn how to delete a task to a list using the HTTP client in an Angular application.
The TaskService
Based on the HTTP protocol, you will use the delete function to add a task to the list: http.delete(). This function expects only 1 parameter, the URL of the resource to delete, including once again the id of the task to delete.
π Instructions
-
Update the task.service.ts file.
Update the TaskFormComponent
Letβs update the TaskFormComponent to use this new function. Th easiest way to do so would be to apply the same logic as in the previous lesson, by updating the submit function to make an API call and navigate to the list page. It would result in the following code:
It works but itβs not the best way to do it as we are duplicating the navigation logic. If the list page path changes, we will have to update it in two places. Letβs handle it by changing the submit function:
- create a variable whose value will be either the updateTask or addTask function, its value will be an Observable you can subscribe to.
- subscribe to this variable and navigate to the list page.
π Instructions
-
Update the task-form.component.ts file.
βοΈ What you learned
You learned how to send a PUT request with HttpClient to update a task in an Angular application.