We added code to get, add, and edit a Todo item by calling various Web API actions, such as GET, POST, and PUT, respectively. Now, let's consume the DELETE action in the Web API to delete a matching Todo item. Follow the given steps to do so:
- Open todo.service.ts.
- Add the deleteTodo function with the following code snippet to delete a matching Todo item by calling the DELETE action in the Web API:
deleteTodo(todo: Todo) {
this.http.delete('/api/todos/' + todo.id)
.subscribe(err => console.log(err),
() => console.log('getTodos Complete')
);
}
This code calls the DELETE action along with the id of the Todo item being deleted. The Web API's DELETE action method will retrieve the matching Todo item from the database and delete it.