include ../../../../_includes/_util-fns .l-main-section :markdown # It Takes Many Heroes Our story needs more heroes. We’ll expand our Tour of Heroes app to display a list of heroes, allow the user to select a hero, and display the hero’s details. Let’s take stock of what we’ll need to display a list of heroes. First, we need a list of heroes. We want to display those heroes in the view’s template, so we’ll need a way to do that. .l-main-section :markdown # Where We Left Off Before we continue with Part 2 of the Tour of Heroes, let’s verify we have the following structure after [Part 1](./toh-pt1.html). If not, we’ll need to go back to Part 1 and figure out what we missed. code-example. angular2-tour-of-heroes ├── node_modules ├── src | ├── app | | └── app.ts | ├── index.html | └── tsconfig.json └── package.json :markdown ## Keep the App Running Start the TypeScript compiler and have it watch for changes in one terminal window by typing pre.prettyprint.lang-bash code npm run tsc :markdown Now open another terminal window and start the server by typing pre.prettyprint.lang-bash code npm start :markdown This will keep the application running while we continue to build the Tour of Heroes. # Displaying Our Heroes ## Creating Heroes Let’s create an array of ten heroes at the bottom of `app.ts`. ``` var HEROES: Hero[] = [ { "id": 11, "name": "Mr. Nice" }, { "id": 12, "name": "Narco" }, { "id": 13, "name": "Bombasto" }, { "id": 14, "name": "Celeritas" }, { "id": 15, "name": "Magneta" }, { "id": 16, "name": "RubberMan" }, { "id": 17, "name": "Dynama" }, { "id": 18, "name": "Dr IQ" }, { "id": 19, "name": "Magma" }, { "id": 20, "name": "Tornado" } ]; ``` The `HEROES` array is of type `Hero`. We are taking advantage of the `Hero` class we coded previously to create an array of our heroes. We aspire to get this list of heroes from a web service, but let’s take small steps on this road and start by displaying these mock heroes in the browser. ## Exposing Heroes Let’s create a public property in `AppComponent` that exposes the heroes for binding. ``` public heroes = HEROES; ``` We did not have to define the `heroes` type. TypeScript can infer it from the `HEROES` array. .alert.is-helpful :markdown We could have defined the heroes list here in this component class. But we know that we’ll get the heroes from a data service. Because know where we are heading, it makes sense to separate the hero data from the class implementation from the start. :markdown ## Displaying Heroes in a Template Our component has`heroes`. Let’s create an unordered list in our template to display them. We’ll insert the following chunk of HTML below the title and above the hero details. ```

My Heroes

``` Now we have a template that we can fill with our heroes. ## Iterating Over Our Heroes with `ng-for` We want to bind the array of `heroes` in our component to our template, iterate over them, and display them individually. We’ll need some help from Angular to do this. Let’s do this step by step. First modify the `
  • ` tag by adding the built-in directive `*ng-for`. ```
  • ``` .alert.is-important :markdown Do not neglect the leading asterisk (`*`) in front of `ng-for`! Although it is not part of the `ng-for` directive name, it is a critical part of the syntax in this usage of `ng-for`. :markdown The (`*`) prefix to the `ng-for` indicates that the `
  • ` element and its children constitute a master template. The `ng-for` directive iterates over the `heroes` array returned by the `AppComponent.heroes` property and stamps out instances of this template. The quoted text assigned to `ng-for` means “*take each hero in the `heroes` array, store it in the local `hero` variable, and make it available to the corresponding template instance*”. The `#` prefix before "hero" identifies the `hero` as a local template variable. We can reference this variable within the template to access a hero’s properties. .alert.is-helpful :markdown Learn more about `ng-for` and local template variables in the [Template Syntax](../guide/template-syntax.html#ng-for) chapter. :markdown With this background in mind, we now insert some content between the `
  • ` tags that uses the `hero` template variable to display the hero’s properties. code-example. <li *ng-for="#hero of heroes"> <span class="badge">{{hero.id}}</span> {{hero.name}}</a> </li> :markdown ## Declaring NgFor When we view the running app in the browser we see nothing … no heroes. We open the developer tools and see an error in the console. ``` EXCEPTION: Can't bind to 'ngForOf' since it isn't a known property of the '