Tutorial: Creating an Angular 2.0 Todo App


With Angular 2.0 right around the corner, big changes are in store. If you would like to understand the differences between Angular 1 and Angular 2 check out this blog post! For the past few years, building a Todo Application has been used as the “Hello World” of understanding a new framework, with just enough meat to get you going. If you already understand Angular 2.0 and just want a working example, visit the finished application here.

Disclaimer: Angular 2.0 is currently in Beta and the syntax is subject to change.

In this tutorial you will:

  • Explore the base of an Angular 2.0 application
  • Create and Angular 2.0 component
  • Add a new route
  • Implement Todo add/remove logic

Step 1: The Base

Angular 2.0 is written in TypeScript, a superset of the ES6 javascript standards. Although TypeScript is not required to build an Angular 2.0 application, the following examples will be written exclusively in TypeScript.

In order to get started the right foot, this tutorial with begin with a blank Angular 2.0 template. This includes a gulp build and the configurations you need to transpile the TypeScript. Since the usage of these common build tools are out of the scope of the this blog post, you might want to read up on Gulp, TSD, and Karma.

To get started, run the following commands:

Step 2: Creating a new Component

First let’s create a new directory called ‘todo' under ‘app/components.' In the ‘todo' directory add two new files called ‘todo.component.ts' and ‘todo.html,’ respectively. Combined, these two files will make up our component.


Explanation of todo.component.ts

The ‘@Component' annotation defines three properties: ‘selector', ‘templateUrl', and ‘directives.’ The ‘selector' property is the name that the component will bind to. In this case, whenever the ‘<todo>' tag is used, this component will be injected. As you might have guessed, the ‘templateUrl' defines the html template that will be injected in place of the selector tag. Finally, the ‘directives' property defines any external directives that are used in the template. In this case, we import and use ‘CORE_DIRECTIVES.’ This includes the basic directives built into Angular 2.0. The second part of this file defines the TypeScript class that will bind to the html file. In this case, we define two placeholder methods which we will fill in later.


Explanation of todo.html

This HTML file, although mostly bootstrap style has a few key Angular 2.0 elements:

<form (submit)="add(newtodo.value)">

When the form is submitted, the add function will be called with the value of the local variable ‘newtodo.’ This local variable is bound to the input text box.

*ng-for="#todoitem of todos">

Using the core directive, ‘*ng-for' we can iterate through the list of todos and assign each to an element ‘todoitem.’ This is very similar to an ‘ng-repeat' in Angular 1.x.

<button id="todo-remove" (click)="remove(todoitem)"

On the click of the button, the ‘remove’ function is called with the selected ‘todoitem' in the list

Step 3: Adding a Route

Let’s add a new route for our Todo Page. In Angular 2.0, routes are added using the ‘@RouteConfig' annotation. Locate ‘app.ts' in the project, and add:

{ path: '/todo', component: TodoComponent, as: Todo }

Since we are using a new component in ‘app.ts' we also need to be sure to import the component:

import {TodoComponent} from '../todo/todo.component';

When you are completed, ‘app.ts' should look like this:

Then, add the link to the navbar. After editing, ‘app.html' should look like this:

Step 4: Adding the Todo Logic

Add these functions to ‘todo.component.ts'


The add function is fairly straightforward, as we push the input string onto the array. We return false from this function so that we can also use the ‘Enter’ key to submit a todo. The remove function finds the first occurrence of to string and removes it from the array.

Wrap up

Congratulations on making your first Angular 2.0 Todo Application! You now understand the basic concepts of building TypeScript, creating a component, and setting up a route. If you run into any issues, please leave a comment, or submit an issue on GitHub.

About the Author

Jake Partusch profile.

Jake Partusch

Sr. Consultant

Jake is a passionate web developer, a relentless tester, and a hobby hardware hacker. Jake has been a full-stack developer for over 4 years and has developed applications using Java, Spring, and AngularJS.

One thought on “Tutorial: Creating an Angular 2.0 Todo App

  1. Kate says:

    Hi Jake!

    Thanks for your tutorial, although I am sorry to say I am not able to use it.

    The code for todo.component.ts doesn’t seem to be working, it gave me 5 errors right away:

    Error:(1, 1) TS1148: Cannot compile modules unless the ‘–module’ flag is provided.
    Error:(1, 42) TS2307: Cannot find module ‘angular2/core’.
    Error:(3, 1) TS1205: Decorators are only available when targeting ECMAScript 5 and higher.
    Error:(10, 14) TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Specify ‘–experimentalDecorators’ to remove this warning.
    Error:(13, 26) TS2355: A function whose declared type is neither ‘void’ nor ‘any’ must return a value or consist of a single ‘throw’ statement.

  2. Asit says:

    I followed all steps it was working smooth but after adding logic its not working .

    1. Hi Asit,

      What error(s) are you getting after adding those two functions?

Leave a Reply to Kate Cancel reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]
Develop Quality Code
As software continues to dominate every facet of our lives, developers are faced with an ever-increasing pressure to produce bug free code. The responsibility of clean quality software falls upon everyone that is involved in […]
Clean JSX structuring in React
A video guide on how to structure your JSX to make it easier to know when it’s appropriate to split out new components. This will help keep your React code smaller, cleaner, and more DRY.
Wrapping a GraphQL Service With a REST API
Use Case Background Providing a GraphQL service on top of REST APIs is a typical GraphQL use case (e.g., https://graphql.org/blog/rest-api-graphql-wrapper/). Doing so can dramatically improve efficiency and flexibility, allowing data consumers to request only the data […]