ngHeroes
  • πŸ‘€About
  • #0: πŸ’ƒ Introduction
  • #1: βŒ› Installations
  • #2: πŸ…° Angular kicks in
  • #3: πŸ“ Component
  • #4: ✏ A new component
  • #5: πŸ’Ό Class
  • #6: πŸ“₯ Property binding
  • #7: πŸ“€Event binding
  • #8: πŸ“Ž Element ref - #
  • #9: πŸ“‹ The To Do list
  • #10: βž• New component: todo-item
  • #11: β›“ Interface
  • #12: πŸ“ŒAdd items
  • #13: 🚧 Refactor App Component
  • #14: πŸ’… Adding Style
  • #15: πŸ”‹ Creating a Service
  • #16: 🎁 Add Items Using the Service
  • #17: πŸ’ΎLocal storage
  • #18: πŸ—‘ Remove item
  • #19: πŸ”˜ Adding a checkbox
  • #20: πŸ›° Deploy to GitHub Pages
  • #21: πŸ’ͺ Enrich the todo-item component
  • Appendix 1: Generating a new project
  • Appendix 2: Tutorial Extensions
Powered by GitBook
On this page

Was this helpful?

#10: βž• New component: todo-item

We will create a new component which will be used for each todo item that is displayed on the list. It will be a simple component at first, but it will grow later on. What's important is that it will get the todo item as an input from its parent component. This way it can be a reusable component, and not rely directly on the application's data and state.

Create a new component called todo-item:

ng g c todo-item

You can see a new folder was created - src/app/todo-item, with the component files inside.

Use the new component in the template of app-root component - inside the <li> element:

src/app/app.component.ts
<ul>
  <li *ngFor="let item of todoList">
    <app-todo-item></app-todo-item>
  </li>
</ul>

Check out the result in the browser. What do you see? Why?

@Input()

We want to display the title of each item within the todo-item component. We need to pass the current item in the loop to the todo-item component.

Again, Angular makes it really easy for us, by providing the Input decorator.

Inside the newly generated TodoItemComponent class in todo-item.component.ts add the line:

src/app/todo-item/todo-item.component.ts
@Input() item;

It tells the component to expect an input and to assign it to the class member called item. Make sure that Input is added to the import statement in the first line in the file. Now we can use it inside the todo-item template and extract the item's title with interpolation: {{ item.title }}

The component should look like this now:

src/app/todo-item/todo-item.component.ts
import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'app-todo-item',
  template: `
    {{ item.title }}
  `,
  styleUrls: ['./todo-item.component.css']
})
export class TodoItemComponent implements OnInit {
  @Input() item;

  constructor() { }

  ngOnInit() {
  }

}

Now we need to pass an item where we use the component. Go back to app-root component and pass the item title to the todo-item:

src/app/app.component.ts
<ul>
  <li *ngFor="let todoItem of todoList">
    <app-todo-item [item]="todoItem"></app-todo-item>
  </li>
</ul>

The item here in square brackets is the same as declared as the component's @Input.

We used property binding on an element we created ourselves! And now we can actually see and understand that property binding binds to an actual property of the component. Soon we'll see how this list can be dynamic.

Previous#9: πŸ“‹ The To Do listNext#11: β›“ Interface

Last updated 6 years ago

Was this helpful?