Todo List Tutorial
Primary version
Primary version
  • Welcome to the ngGirls tutorial!
  • Workshop: Todo List
    • πŸ‘€About
    • #0: πŸ’ƒ Introduction
    • #1: βŒ› Installations
      • a. StackBlitz instructions
    • #2: πŸ…° Angular kicks in
    • #3: πŸ“ Component
    • #4: ✏ A new component
      • a. StackBlitz instructions
    • #5: πŸ’Ό Class
    • #6: πŸ“₯ Property binding
    • #7: πŸ“€Event binding
    • #8: πŸ“Ž Element ref - #
      • a. About
      • b. Explore
      • c. Resources
    • #9: πŸ“‹ The To Do list
    • #10: βž• New component: todo-item
    • #11: β›“ Interface
      • a. StackBlitz instructions
    • #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
    • #21: πŸ’ͺ Enrich the todo-item component
    • Appendix 1: Git and GitHub
    • Appendix 2: πŸ›° Deploying your app
      • Deploy to Azure Static Web Apps
      • Deploy to GitHub Pages
        • a. StackBlitz instructions
    • Appendix 3: Tutorial Extensions
    • Appendix 4: Generating a new project
    • Troubleshooting
      • Installation
  • More Workshops
    • Second workshops - CRUD and HTTP
      • #1 MongoDB
      • #2 Local server
      • #3 http in diagrams
      • #4 POST
      • #5 GET
      • #6 DELETE and PUT
    • Gallery with Rx.js
      • #0: Init photo gallery
      • #1: Observable
      • #2: More interaction! Upload photos
      • #3: Merging Observables
      • #4: Filtering by category
      • #5: Adding photos to category
    • Forms
      • #1: Template-driven forms
      • #2: Reactive forms
      • #3: Form builder
    • NgRx
      • #1: Actions and reducers
      • #2: Store Devtools
      • #3: Implementing actions in app
      • #4: Selectors
    • RxJS
      • Operators
      • Play time!
    • Blog Editor
      • #1 βš™οΈConfiguring firebase
      • #2 πŸ’…Add Angular Material
      • #3 πŸ’…Add Bootstrap
      • #4 🧭Add Navigation
      • #5 ✏️Add Editor
      • #6 βš™οΈConnect Database
      • #6 πŸ’…Add Feed
      • #7 ✏️Edit Post
      • #9 πŸš€Deploy
      • #10 ✏ ️Next Steps
Powered by GitBook
On this page
  1. Workshop: Todo List

#11: β›“ Interface

Previous#10: βž• New component: todo-itemNexta. StackBlitz instructions

Last updated 1 year ago

We want to use TypeScript's abilities to know what kind of object we pass as an item to the todo-item component. We'll make sure that the item is of the right type. But its type is not a simple string, number or boolean. We'll define the item's type using an interface.

We've already seen an interface provided by Angular: the OnInit interface which includes the method ngOnInit. Every Class that implements this interface should define this method. Otherwise we'll get an error during compile time.

Interfaces exist only in TypeScript and are removed when the code is compiled to JavaScript. In JavaScript we cannot enforce type safety out of the box.

Create a TodoItem interface in a new interfaces folder with the Angular CLI:

ng g i interfaces/todo-item

i is short for... you guessed it - interface. Adding a path in the command to the Angular CLI generates the folders you specified if they do not already exist.

StackBlitz Instructions

We'll use the Angular Generator to create the interface. Follow the instructions on the page and return here to continue the worksheet.

In your IDE, open the newly created file src/app/interfaces/todo-item.ts:

src/app/interfaces/todo-item.ts
export interface TodoItem {
}

Now we can define what properties and/or methods every object of type TodoItem should have. At this point we'll add two members:

  • title which must be of type string

  • completed which is of type boolean and is an optional member

src/app/interfaces/todo-item.ts
export interface TodoItem {
  title: string;
  completed?: boolean;
}

Let's define the item @Input to be of the type we've created. This will allow the IDE to suggest us available members when we use item in the component class and template.

src/app/todo-item/todo-item.component.ts
export class TodoItemComponent implements OnInit {
  @Input() item: TodoItem;

You need to import the interface to this file.

src/app/todo-item/todo-item.component.ts
import { TodoItem } from '../interfaces/todo-item';

Now, let's define the list of todo items to contain objects of the TodoItem type.

src/app/app.component.ts
export class AppComponent {
  title = 'todo-list';
  todoList: TodoItem[] = [
    {title: 'install NodeJS'},
    {title: 'install Angular CLI'},
    {title: 'create new app'},
    {title: 'serve app'},
    {title: 'develop app'},
    {title: 'deploy app'},
  ];
}

Again, you need to import the interface to this file.

src/app/app.component.ts
import { TodoItem } from './interfaces/todo-item';

Now try to delete the title of one of the objects in the list. What happens?

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

See the results on StackBlitz
StackBlitz instructions