Angular 2 - Hello World



In the previous chapter, we studied how to setup development environment for Angular 2. In this chapter let us create an example to display Hello World text.


The below example describes how to display a simple text in the Angular 2:

<!DOCTYPE html>
    <title>Hello World</title>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
        transpiler: 'typescript',
        typescriptOptions: { emitDecoratorMetadata: true },
        packages: {'app': {defaultExtension: 'ts'}},
        map: { 'app': './angular2/src/app' }
            .then(null, console.error.bind(console));

The above code includes the following configuration options:

  • You can configure the index.html file using typescript version. The SystemJS transpile the TypeScript to JavaScript before running the application by using the transpiler option.

  • If you do not transpile to JavaScript before running the application, you could see the compiler warnings and errors which are hidden in the browser.

  • The TypeScript generates metadata for each and every class of the code when the emitDecoratorMetadata option is set. If you don't specify this option, large amount of unused metadata will be generated which affects the file size and impact on the application runtime.

  • Angular 2 includes the packages form the app folder where files will have the .ts extension.

  • Next it will load the main component file from the app folder. If there is no main component file found, then it will display the error in the console.

  • When Angular calls the bootstrap function in main.ts, it reads the Component metadata, finds the 'app' selector, locates an element tag named app, and loads the application between those tags.

To run the code, you need the following TypeScript(.ts) files which you need to save under the app folder.

import {bootstrap} from "angular2/platform/browser"
import {MyHelloWorldClass} from "./hello_world_app.component"


Now we will create a component in TypeScript(.ts) file as shown below:

import {Component, View} from "angular2/core";

   selector: 'my-app'

  template: '<h2>Hello World !!</h2>'

export class MyHelloWorldClass {

  • The @Component is a decorator which uses configuration object to create the component.

  • The selector creates an instance of the component where it finds <my-app> tag in parent HTML.

  • The @view contains a template that tells Angular how to render a view.

  • The export specifies that the component will be available outside the file.


Let's carry out the following steps to see how above code works:

  • Save above HTML code as index.html file as how we created in environment chapter and use the above app folder which contains .ts files.

  • Open the terminal window and enter the below command:

    npm start
  • After few moments, a browser tab should open and displays the output as shown below.

OR you can run this file in another way:

  • Save above HTML code as angular2_hello_world.html file in your server root folder.

  • Open this HTML file as http://localhost/angular2_hello_world.html and output as below gets displayed.