#12: πŸ“ŒAdd items

We want to add items to our list. With Angular, we can do this easily and see the item added immediately. We will do this from within the input-button-unit component we created earlier. We'll change it so when hitting the Enter key or clicking the submit button, the value of the input box will become the title of the new item, and the new item will be added to the list.

But we don't want the input-button-unit component to be responsible for adding a new item to the list. We want it to have minimal responsibility, and delegate the action to its parent component. One of the advantages of this approach is that this component will be reusable, and can be attached to a different action in different situations.

For example, in our case, we'll be able to use the input-button-unit inside the todo-item component. Then we'll have an input box for each item and we'll be able to edit the item's title. In this case, pressing the Enter key or the Save button will have a different effect.

So what we actually want to do is to emit an event from the input-button-unit component whenever the title is changed. With Angular, we can easily define and emit events from our components!

@Output()

Add the following line inside the InputButtonUnitComponent Class, which defines an output for the component:

src/app/input-button-unit/input-button-unit.component.ts
@Output() submit: EventEmitter<string> = new EventEmitter<string>();

The output property is called submit. It's of type EventEmitter which has the method emit. EventEmitter is a Generic Type - we pass to it another type which will be used internally, in this case it's string. It's the type of the object that will be emitted by the emit method.

Make sure that Output and EventEmitter are added to the import declaration in the first line of the file:

src/app/input-button-unit.component.ts
import { Component, OnInit, Output, EventEmitter} from '@angular/core';

Now, whenever we call this.submit.emit(), an event will be emitted to the parent component. Let's call it in the changeTitle method:

src/app/input-button-unit/input-button-unit.component.ts
changeTitle(newTitle: string): void {
  this.submit.emit(newTitle);
}

We delegate everything to the parent component - even actually changing the title of the item if needed.

We pass newTitle when we emit the event. Whatever we pass in emit() will be available for the parent as $event. The events emitted from keyup.enter and click still call the same method, but the method itself has changed.

The method name no longer matches the action it provides. Let's change it to something more appropriate: submitValue. You can use the IDE tools to refactor the method name - make sure that it is changed in the template as well.

src/app/input-button-unit/input-button-unit.component.ts
submitValue(newTitle: string) {
  this.submit.emit(newTitle);
}
src/app/input-button-unit/input-button-unit.component.ts
template: `
  <input #inputElementRef
         [value]="title"
         (keyup.enter)="submitValue(getInputValue($event))">

  <button (click)="submitValue(inputElementRef.value)">
    Save
  </button>
`,

Listening to the event

Now all we need to do is catch the event in the parent component and attach logic to it. Go to the app-root component and bind to the submit event in the <app-input-button-unit> component:

src/app/app.component.ts
<app-input-button-unit (submit)="addItem($event)"></app-input-button-unit>

Now all that's left is to implement the addItem method, which receives a string, creates an object with the string as the title property, and adds it to the list:

src/app/app.component.ts
addItem(title: string) {    
  this.todoList.push({ title });
}

Try it out - enter a new todo item title in the input field and submit it!

Note: We're using ES6 Object Property Value Shorthand to construct the todo item object. If we use a variable with the same name of the object's property to which we want to assign the variable's value to, we can use this shorthand notation. In our case, { title } is equivalent to { title: title }. If the string was stored in a variable with a different name, we couldn't have used the shorthand. For example:

code for example
addItem(value: string) {    
  this.todoList.push({ title: value });
}

πŸ’Ύ 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

Last updated