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?

#13: 🚧 Refactor App Component

We're going to perform a small refactoring. The app-root shouldn't have such a large template and all this logic. It should just call another component that will deal with that.

  • Create a new component called list-manager:

ng g c list-manager
  • Move all the code from app-root to list-manager.

  • You can keep the title in app-root, and give it a nice value.

  • Be careful not to change the list manager component's class name!

src/app/app.component.ts
@Component({
  selector: 'app-root',
  template: `
    <h1>
      Welcome to {{ title }}!
    </h1>
  `,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'My To Do List APP';
}
src/app/list-manager/list-manager.component.ts
import { Component, OnInit } from '@angular/core';
import { TodoItem } from '../interfaces/todo-item';

@Component({
  selector: 'app-list-manager',
  template: `
    <app-input-button-unit (submit)="addItem($event)"></app-input-button-unit>

    <ul>
      <li *ngFor="let todoItem of todoList">
        <app-todo-item [item]="todoItem"></app-todo-item>
      </li>
    </ul>
  `,
  styleUrls: ['./list-manager.component.css']
})
export class ListManagerComponent implements OnInit {
  todoList: TodoItem[] = [
    {title: 'install NodeJS'},
    {title: 'install Angular CLI'},
    {title: 'create new app'},
    {title: 'serve app'},
    {title: 'develop app'},
    {title: 'deploy app'},
  ];

  constructor() { }

  ngOnInit() {
  }

  addItem(title: string) {    
    this.todoList.push({ title });
  }
}
  • Call the new component from the app-root template:

src/app/app.component.ts
  template: `
    <h1>
      Welcome to {{ title }}!
    </h1>

    <app-list-manager></app-list-manager>
  `,

That's it! Now we can go on.

Previous#12: πŸ“ŒAdd itemsNext#14: πŸ’… Adding Style

Last updated 6 years ago

Was this helpful?