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:

&lt;form <span class="hljs-list">(<span class="hljs-keyword">submit</span>)</span>=<span class="hljs-string">"add(newtodo.value)"</span>&gt;

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-<span class="hljs-keyword">for</span>=<span class="hljs-string">"<span class="hljs-subst">#todoitem</span> of todos"</span>&gt;

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.

&lt;button id=<span class="hljs-string">"todo-remove"</span> <span class="hljs-list">(<span class="hljs-keyword">click</span>)</span>=<span class="hljs-string">"remove(todoitem)"</span>

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:

{ <span class="hljs-attribute">path</span>: <span class="hljs-string">'/todo'</span>, <span class="hljs-attribute">component</span>: TodoComponent, <span class="hljs-attribute">as</span>: Todo }

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

<span class="hljs-keyword">import</span> {TodoComponent} <span class="hljs-keyword">from</span> <span class="hljs-string">'../todo/todo.component'</span>;

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

Object Partners profile.

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.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]