Angular 2 Tutorial: Angular 2.0 in 20 minutes

This article is outdated. Check out my brand new Angular 4 tutorial on YouTube.

Angular 2 has been getting a lot of momentum lately. The number of tweets and posts have been increasing as Angular team have been preparing the release candidate. So, I’m planing to write a series of hands-on blog posts to help you get started with Angular 2.0. If you have any questions as you read this article, please drop a comment below. I’ll answer every question and update the post if required.

Whether you’re familiar with Angular 1 or not, it doesn’t matter. You just need to have basic familiarity with HTML, CSS and Javascript.

Let’s start by a few beginners’ questions.

Beginners’ questions

If you’re already familiar with Angular 1, feel free to skip this section.

What is Angular? 

Angular is one of the leading frameworks for building well-structured, testable and maintainable front-end applications. It’s often (but not necessarily) used in building single page applications (SPAs).

Why do I have to learn Angular?

Angular is not the only framework for building modular, testable and maintainable front-end applications. There are many other frameworks and libraries out there, including but not limited to React, Ember, Backbone, etc. Do you have to learn them all? If you have time and passion, why not! But if you have limited time, you may better invest your time in learning Angular 2.0 and React as these are the leading frameworks in this space.

Angular is developed by Google and has a huge community support. Google trends shows that the demand for Angular developers is increasing constantly.

angular developer

So, whether you’re an aspiring or an established front-end or full-stack developer, knowing how to build applications with Angular can definitely increase your options when it comes to finding that ideal job.

Is Angular better than “…”?

The problem with this kind of question is that it’s hard to define “better”. Every framework has certain strengths and weaknesses. Religious debates about which framework is “better” than the others are really useless and don’t give you any values.

So, instead of wasting your time researching the best framework out there, spend some time and learn the top 2 – 3 leading frameworks. Then, you can pick the one that works best for you.

Getting the tools

If you don’t have Node on your machine, first, head over to and download and install the latest version of Node.

Once you install Node, open up Command Prompt on Windows or Terminal on Mac, and run:

npm install -g typescript

If you are a Mac user, you need to put sudo before npm to run this command with administrative privileges.

This will install TypeScript globally on your machine. TypeScript is a superset of Javascript and is the language we will be using in this tutorial. Why TypeScript? Because it brings many useful features to Javascript that are missing in the current version of Javascript, including classes, interfaces, access modifiers (e.g. public, private), IntelliSense and more importantly compile-time checking. So, we can catch many programming errors during compile time. Angular 2.0 itself has been written with TypeScript.

Just note that browsers don’t understand TypeScript. We use TypeScript compiler to compile or more accurately transpile TypeScript into Javascript. More on this later.

Next, run:

npm install -g typings

Typings is another Node module that we use to reference existing Javascript libraries in TypeScript.

Finally, you need an editor. You can use any editors that support TypeScript, including VSCode, Sublime, Atom, IntelliJ IDEA, Visual Studio and etc.

Your first Angular 2 app

Download the seed project and extract it somewhere on your machine. Inside this project, you’ll find a few  configuration files, an index.html and an app folder, which includes the source files for our application.

Inside the app folder, we have a couple of TypeScript files: boot.ts, which is the main or starting module of our application, and app.component.ts, which is the root component of our application. Every Angular 2 app has at least one component, which we call root component.

tsconfig.json is the configuration file TypeScript compiler uses to determine how to transpile our TypeScript code into Javascript.

typings.json is another configuration file for Typescript. When using external Javascript libraries in TypeScript, we need to import a typescript definition file. A type definition file gives us static type checking and IntelliSense for that Javascript library.

Next to that, we have package.json, which is a standard Node package configuration file, where we define the dependencies of our app.

  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5"

Now, we need to install these dependencies. So, open up Command Prompt on Windows or Terminal on Mac, and go to the folder where you’ve extracted the seed project. Then, run:

npm install 

This is going to take several minutes for the first time, so be patient. If you get several errors, that’s most likely due to administrative privileges. So, on Mac, be sure to add sudo before npm. 

Once these dependencies are installed, you’ll see a new folder called node_modules. All our application dependencies will be stored there.

Now, have one more look at package.json. Under the scripts section, we can have a few custom node commands:

"scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 

The one we’ll be using a lot is start.

    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",  


It’s a shortcut for concurrently running two commands:

  • npm run tsc:w: which runs TypeScript compiler in the watch mode. When we save a TypeScript file, TypeScript compiler will automatically detect the changes and transpile our TypeScript code into Javascript. We never have to view or modify these Javascript files. So we code purely in TypeScript.
  • npm run lite: this will run the lite web server for our Angular app.

Now, back in the terminal and run the following command from the project folder:

npm start

When the lite web server starts, it’ll launch your default browser navigating to http://localhost:3000. This is our first Angular 2.0 app.


What is a component?

A component is one of the most fundamental building blocks in Angular 2 apps. Every app consists of at least one component, which we call the root component. A component can include other components, which we call child components. A real-world app is essentially a tree of components.

But what are these components really? A component is a TypeScript class that encapsulates the template, data and behaviour for a view. So, it’s more accurate to call it a view component. That’s what they’re called in React.

As an example, think of Twitter. If you want to build a similar app in Angular 2, you may model your application components like this:

  • app
    • navbar
    • sidebar
    • content
      • tweets
        • tweet

Root component

Open up app/app.component.ts. This is the root component of our app:

import {Component} from 'angular2/core';

    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>' 
export class AppComponent { }

As you see, a component is a TypeScript class decorated with @Component decorator. We use decorators (also called annotations) to add metadata to a class. This @Component decorator tells Angular that AppComponent is a component. Note that these decorators are actually functions. So, here, @Component is called and given an object which includes metadata about AppComponent:

    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>' 

This object has two properties: template, which specifies the markup that should be rendered when this component is used, and selector, which tells Angular where in the DOM it should render this component. This is a CSS selector. So, my-app here represents an element with the name my-app.

When Angular sees an element like that in the DOM, it’ll create an instance of AppComponent and render it (according to its template) inside that element. Open up index.html and scroll down a bit. Note the my-app element.

Rendering data

Back in app.component.ts, let’s define a field in this class:

export class AppComponent { 
	title = "Hello World";

So, as I explained earlier, a component encapsulates the template, data and the behaviour for a view. We use fields to store data. Now, let’s render the value of this field in the template. Modify the template as follows:

    template: 'Hello {{title}}' 

We use double curly braces syntax, called interpolation, to render data.

Now, save the file. Since TypeScript compiler is running in the background, it will re-compile our AppComponent. Our lite web server uses a module called BrowserSync, which automatically refreshes the browser when it detects a change in the source files. So, if you switch back to your browser, you should see the new content.

Handling events

Let’s extend our component and add a button. First, replace the single quote character in the template with backtick. That’s the character to the left of number 1 on your keyboard. By using backtick, we can break up our template into multiple lines and make it more readable.

    selector: 'my-app',
    template: `
<h1>Hello {{title}}</h1>

Now, add a button and a span to the template:

<h1>Hello {{title}}</h1>
<button>Click me</button>

We want to display a counter on this view. Every time we click the button, the counter will be increased by one. So, first declare a new field in the component:

export class AppComponent { 
        count = 0;
        title = "Hello World";

Then, modify the span and use interpolation to render the value of count:

<span>Clicks: {{count}}<span>

Finally, to handle the click event, we need to bind it to a method in our component. When we click the button, the corresponding method in the component will be called.

Change the button declaration as follows:

<button (click)="increaseCount()">Click me</button>

Note the syntax. This is called event binding. So, we put the event name in parentheses and then set to a method in the component.

Now, let’s create this method:

export class AppComponent { 
	count = 0;
	title = "Hello World";


Save the file and switch back to your browser. Click the button a few times and note the counter.

So, in this article, you learned the basics of components in Angular 2.0 apps. A component encapsulates the template, data and behaviour of a view. We used interpolation to render data and event binding to handle events raised from DOM elements.

In the next part, we’ll look at services and dependency injection. If you want to be notified when I publish the next part, subscribe to my blog below.

If you enjoyed this tutorial, please drop a comment below and share it with others.

You can also check out my comprehensive Angular 2 course here.

logo draft 2_3_2048

Hi, my name is Mosh Hamedani and I am the author of several best-selling courses on Udemy and Pluralsight with more than 130,000 students in 196 countries. You can see the list of all my web and mobile development courses on this website.

Related Posts


22 responses to “Angular 2 Tutorial: Angular 2.0 in 20 minutes”

  1. Recep says:

    I have never tried to build a website before with Angular. But As I can see, It’s very simple and very useful to build website.

  2. Ganesh says:

    Hi Mosh.

    I had asked you earlier about the course, that you are going to be releasing on PS soon. Please, may I know the tentative date of release? Thanks

    Great article, by the way, as always…

  3. David Ng says:

    Would be nice to explain a bit more about the functions of other files.

  4. Jay says:

    Hi Mosh

    Your video course are awesome. Thank you for providing such high quality videos.

    I have quick question, do you mind to answer me please.

    There is a request, I need call angular 2 function from outside(eg. chrome debug console). In angular 2 debug mode, there’s a API called ng.probe() can return the component I needed. but ng.probe() will be disabled once turn angular 2 into production model.

    Do you have any ideal how to call angular 2 functions in chrome debug console?


  5. Pankaj Parkar says:

    Thanks for quick, it did really understand & run Angular 2 simple application on my Macbook.

  6. Carlos says:

    Very thanks Mosh, that’s a great and simple article to initiate into Angular, I also took .NET Developer in PS, they are amazing.

  7. Ken says:

    Very clear — setup as well as code! Thanks.

  8. coder says:

    Unfortunately I tried many many (tons) of “get started angular2” examples and no one worked. Error is the same

    tried all fixes and workarounds.

    Now i’m oing to tell you something. This technology will fail because of a lot of mess with dependencies, versions, modules, configs. It’s the dead end!!

  9. learningangular2 says:

    Hi Mosh,

    I am very new to angular2 . I followed the steps . When I run the project, I only see Loading … I am not seeing the component being loaded. Have I missed something ? How can I get this to work ?

    Thank you

  10. Mmpt says:

    Straight to the point tutorial. Thanks!

  11. Manoj says:

    Really an eye opener for folks who are using Angular 1 and who thinks Angular 2 is a hard nut to crack.This tutorial made me to learn Angular 2 from scratch and made me to stay focused throughout the application without losing any interest.Thanks Mosh 🙂

  12. Manoj says:

    Hi Mosh can you explain why this code is been used?

    packages: {
    app: {
    format: ‘register’,
    defaultExtension: ‘js’
    .then(null, console.error.bind(console));

    Can we run the app without this code,using a grunt/gulp task?

  13. Foysal says:

    Should we learn Typescript to be a good AngularJS 2 Developer ??

  14. Rahul says:

    Hi Mosh,

    Awesome Article 🙂

    But i have a little doubt. I cant run “tsc” command in terminal. It shows “command not found” error.
    I have already installed typescript. It has been installed at “/Users/rahul/.npm-global/lib”.

    But when i try out your style by putting “node-modules” in project folder. It is compiling typescript code.

    Please help in this regard 🙂


  15. sunil says:


    From one month I am trying to learn A2,But I am facing an issue i.e., “HOW TO DEBUG A2” from “VS CODE”, in Lanch.json what is this line indicate “program”: “${workspaceRoot}/node_modules/lite-server/bin/lite-server”.
    And even after running application unable to attach to process any guidance to debug the apps.

    Thanks, INADVANCE.

  16. Emerson Jose says:

    Hey Mash,

    I liked so much this tutorial in little words you can example all basic steps to getting started with Angular 2. Thank you so much and keep going with this work!

  17. abdul says:

    Hi Hamedani,

    I am thankfull to you for sharing such a good tutorial like this.


  18. Rahul Arora says:

    I was having trouble finding a nice and simple tutorial on Angular but I must say your tutorial is amazing. Really helpful. Thanks.

  19. Anat says:

    The artical is very clear and understandable
    Thank you, Mosh for your execelent explanations

  20. dave says:

    Many thanks for your tutorial! It’s really well made and easy to understand 🙂

  21. Vikrant says:

    Hi Mosh,
    Thanks for the nice tutorial, i was wondering that can you please create a tutorial/video on how to create an with MVC4 using Angular2.0 without node or npm.

Leave a Reply