When you're done with this page, the app should look like this live example.

Keep the app transpiling and running

Enter the following command in the terminal window:

npm start

This command runs the TypeScript compiler in "watch mode", recompiling automatically when the code changes. The command simultaneously launches the app in a browser and refreshes the browser when the code changes.

You can keep building the Tour of Heroes without pausing to recompile or refresh the browser.

Show the hero

Add two properties to the AppComponent: a title property for the app name and a hero property for a hero named "Windstorm."

Hero object

The hero needs more properties. Convert the hero from a literal string to a class.

Create a Hero class with id and name properties. Add these properties near the top of the app.component.ts file, just below the import statement.

src/app/app.component.ts (Hero class)

export class Hero {
id: number;
name: string;
}

In the Hero class, refactor the component's hero property to be of type Hero, then initialize it with an id of 1 and the name Windstorm.

src/app/app.component.ts (hero property)

hero: Hero = {
id: 1,
name: 'Windstorm'
};

Because you changed the hero from a string to an object, update the binding in the template to refer to the hero's name property.

template: `<h1>{{title}}</h1><h2>{{hero.name}} details!</h2>`

The browser refreshes and continues to display the hero's name.

Adding HTML with multi-line template strings

To show all of the hero's properties, add a <div> for the hero's id property and another <div> for the hero's name. To keep the template readable, place each <div> on its own line.

The backticks around the component template let you put the <h1>, <h2>, and <div> elements on their own lines, thanks to the template literals feature in ES2015 and TypeScript. For more information, see Template literals.

Two-way binding

[(ngModel)] is the Angular syntax to bind the hero.name property to the textbox. Data flow in both directions: from the property to the textbox; and from the textbox back to the property.

Unfortunately, immediately after this change, the application breaks. If you looked in the browser console, you'd see Angular complaining that "ngModel ... isn't a known property of input."

Although NgModel is a valid Angular directive, it isn't available by default. It belongs to the optional FormsModule. You must opt-in to using that module.

Import the FormsModule

Open the app.module.ts file and import the FormsModule symbol from the @angular/forms library. Then add the FormsModule to the @NgModule metadata's imports array, which contains the list of external modules that the app uses.

The road ahead

In the next tutorial page, you'll build on the Tour of Heroes app to display a list of heroes. You'll also allow the user to select heroes and display their details. You'll learn more about how to retrieve lists and bind them to the template.