#16: 🎁 Add Items Using the Service

Let's improve our service by adding more abilities that will be used by our components. First - we'll implement adding an item to the list.

Adding an item

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

addItem(item): void {

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

addItem(title): void {
this.todoListService.addItem({ title });
  • Note that the service's method expects the whole item, while the component's method expects only the title and constructs the item. (You may decide to let the service construct the item from the 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.