#16: 🎁 Add Items Using the Service

In this chapter, we'll improve our service by adding more abilities.

First, lets open our app's service file, which is available at todo-list.service.ts.

There we'll add a new method to the service, called addItem, like so:

todo-list.service.ts
addItem(item): void {
this.todoList.push(item);
}

This will allow us to call the same method from everywhere across the application, thus making our app easier to maintain.

And now we can change our code in list-manager.component.ts to call the addItem method directly from the service like so:

list-manager.component.ts
addItem(title): void {
this.todoListService.addItem({ title });
}
  • There may be additional logic when calling these methods, i.e. saving the changes in a database (which we'll implement later).

  • A better way to handle data is using immutable objects, but that's a bigger topic than we can cover in this tutorial at the moment.