Real application development takes place in a local development environment like your machine.
Follow thesetupinstructions for creating a new project namedangular-tour-of-heroesafter which the file structure should look like this:
tsconfig.json
When we're done with this first episode,the app runs like thislive example.
Keep the app transpiling and running
We want to start the TypeScript compiler,have it watch for changes,and start our server. Do this by entering the following command in the terminal window.
npm start
This command runs the compiler in watch mode,starts the server,launches the app in a browser,and keeps the app running while we continue to build the Tour of Heroes.
Show our Hero
We want to display Hero data in our app
Update theAppComponentso it has two properties: atitleproperty for the application name and aheroproperty for a hero named "Windstorm".
}
Now update the template in the@Componentdecoration with data bindings to these new properties.
'<h1>{{title}}</h1><h2>{{hero}} details!</h2>'
The browser should refresh and display our title and hero.
The double curly braces tell our app to read thetitleandheroproperties from the component and render them. This is the "interpolation" form of one-way data binding.
Uh oh,our template string is getting long. We better take care of that to avoid the risk of making a typo in the template.
Multi-line template strings
We could make a more readable template with string concatenation but that gets ugly fast,it is harder to read,and it is easy to make a spelling error. Instead,let’s take advantage of the template strings feature in ES2015 and TypeScript to maintain our sanity.
Change the quotes around the template to back-ticks and put the<h2>and<div>elements on their own lines.
`
Editing Our Hero
We want to be able to edit the hero name in a textBox.
Refactor the hero name<label>with<label>and<input>elements as shown below:
`
We see in the browser that the hero’s name does appear in the<input>textBox. But something doesn’t feel right. When we change the name,we notice that our change is not reflected in the<h2>. We won't get the desired behavior with a one-way binding to<input>.
Two-Way Binding
We intend to display the name of the hero in the<input>,change it,and see those changes wherever we bind to the hero’s name. In short,we want two-way data binding.
Before we can use two-way data binding forform inputs,we need to import theFormsModulepackage in our Angular module. We add it to theNgModuledecorator'simportsarray. This array contains the list of external modules used by our application. Now we have included the forms package which includesngModel.
Let’s update the template to use thengModelbuilt-in directive for two-way binding.
Replace the<input>with the following HTML
<input [(ngModel)]="hero.name"placeholder="name">
The browser refreshes. We see our hero again. We can edit the hero’s name and see the changes reflected immediately in the<h2>.
The Road We’ve Travelled
Let’s take stock of what we’ve built.
Our Tour of Heroes uses the double curly braces of interpolation (a kind of one-way data binding) to display the application title and properties of aHeroobject.
We wrote a multi-line template using ES2015’s template strings to make our template readable.
We can both display and change the hero’s name after adding a two-way data binding to the<input>element using the built-inngModeldirective.
ThengModeldirective also propagates changes to every other binding of thehero.name.
Our Tour of Heroes only displays one hero and we really want to display a list of heroes. We also want to allow the user to select a hero and display their details. We’ll learn more about how to retrieve lists,bind them to the template,and allow a user to select a hero in thenext tutorial chapter.