Tutorial: Creating an Angular 2.0 Todo App

Introduction

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.

todo.component.ts

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.

todo.html

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'

Explanation

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

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

Related Blog Posts
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]
Using Spring Beans in a Kafka Streams ExceptionHandler
There are many things to know before diving into Kafka Streams. If you haven’t already, check out these 5 things as a starting point. Bullet 2 mentions designing for exceptions. Ironically, this seems to be […]