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.
We'll add a new method to the service, called addItem
, like so:
{% code title="src/app/services/todo-list.service.ts" %}
addItem(item: TodoItem) {
this.todoList.push(item);
}
{% endcode %}
Now we can change our list-manager
component to call the addItem
method directly from the service:
{% code title="src/app/list-manager/list-manager.component.ts" %}
addItem(title: string) {
this.todoListService.addItem({ title });
}
{% endcode %}
- 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.
{% hint style="info" %} 💾 Save your code to GitHub
StackBlitz users - press Save in the toolbar and continue to the next section of the tutorial.
Commit all your changes by running this command in your project directory.
git add -A && git commit -m "Your Message"
Push your changes to GitHub by running this command in your project directory.
git push
{% endhint %}
{% hint style="success" %} See the results on StackBlitz {% endhint %}