Setup to develop locally
Follow the setup instructions for creating a new project
The file structure should look like this:
When you're done with this page, the app should look like this
Keep the app transpiling and running
Enter the following command in the terminal window:
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
title property for the app name and a
for a hero named "Windstorm."
Now update the template in the
@Component decorator with data bindings to these new properties.
The browser refreshes and displays the title and hero name.
The double curly braces are Angular's interpolation binding syntax.
These interpolation bindings present the component's
hero property values,
as strings, inside the HTML header tags.
Read more about interpolation in the Displaying Data page.
The hero needs more properties.
hero from a literal string to a class.
Hero class with
Add these properties near the top of the
app.component.ts file, just below the import statement.
AppComponent class, refactor the component's
hero property to be of type
then initialize it with an
1 and the name
Because you changed the hero from a string to an object,
update the binding in the template to refer to the hero's
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,
<div> for the hero's
id property and another
<div> for the hero's
To keep the template readable, place each
<div> on its own line.
The backticks around the component template let you put the
<div> elements on their own lines,
thanks to the template literals feature in ES2015 and TypeScript. For more information, see
Edit the hero name
Users should be able to edit the hero name in an
The textbox should both display the hero's
and update that property as the user types.
You need a two-way binding between the
<input> form element and the
Refactor the hero name in the template so it looks like this:
[(ngModel)] is the Angular syntax to bind the
to the textbox.
Data flows 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
NgModel is a valid Angular directive, it isn't available by default.
It belongs to the optional
You must opt-in to using that module.
Import the FormsModule
app.module.ts file and import the
FormsModule symbol from the
Then add the
FormsModule to the
imports array, which contains the list
of external modules that the app uses.
AppModule looks like this:
When the browser refreshes, the app should work again.
You can edit the hero's name and see the changes reflected immediately in the
<h2> above the textbox.
The road you've travelled
Take stock of what you've built.
- The Tour of Heroes app uses the double curly braces of interpolation (a type of one-way data binding)
to display the app title and properties of a
- You wrote a multi-line template using ES2015's template literals to make the template readable.
- You added a two-way data binding to the
<input>element using the built-in
ngModeldirective. This binding both displays the hero's name and allows users to change it.
ngModeldirective propagates changes to every other binding of the
Your app should look like this
Here's the complete
app.component.ts as it stands now:
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.