1.4 Angular - Display A Selection List
1.4 Angular - Display A Selection List
1.4 Angular - Display A Selection List
mode_edit
Display a selection list
This tutorial shows you how to:
For the sample application that this page describes, see the live
example / download example.
https://angular.io/tutorial/tour-of-heroes/toh-pt2 1/10
05/07/2023, 16:00 Angular - Display a selection list
src/app/mock-heroes.ts
Skip to main content
Displaying heroes
Open the HeroesComponent class file and import the mock HEROES .
src/app/heroes/heroes.component.ts
heroes = HEROES;
}
https://angular.io/tutorial/tour-of-heroes/toh-pt2 2/10
05/07/2023, 16:00 Angular - Display a selection list
<h2>My Heroes</h2>
<ul class="heroes">
<li>
<button type="button">
<span class="badge">{{hero.id}}</span>
<span class="name">{{hero.name}}</span>
</button>
</li>
</ul>
That displays an error since the hero property doesn't exist. To have
access to each individual hero and list them all, add an *ngFor to the
https://angular.io/tutorial/tour-of-heroes/toh-pt2 3/10
05/07/2023, 16:00 Angular - Display a selection list
SYNTAX DETAILS
Skip to main content
INTERACTIVE ELEMENTS
Inside the <li> element, add a <button> element to wrap the hero's
details, and then make the hero clickable. To improve accessibility, use HTML
In the first tutorial, you set the basic styles for the entire application in
styles.css . That style sheet didn't include styles for this list of heroes.
https://angular.io/tutorial/tour-of-heroes/toh-pt2 4/10
05/07/2023, 16:00 Angular - Display a selection list
You could add more styles to styles.css and keep growing that style
Skip to main content
sheet as you add components.
You may prefer instead to define private styles for a specific component.
This keeps everything a component needs, such as the code, the HTML,
are different.
src/app/heroes/heroes.component.ts (@Component)
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
Open the heroes.component.css file and paste in the private CSS styles
for the HeroesComponent from the final code review.
https://angular.io/tutorial/tour-of-heroes/toh-pt2 5/10
05/07/2023, 16:00 Angular - Display a selection list
Viewing details
When the user clicks a hero in the list, the component should display the
The code in this section listens for the hero item click event and
display/update the hero details.
The parentheses around click tell Angular to listen for the <button>
element's click event. When the user clicks in the <button> , Angular
executes the onSelect(hero) expression.
starts.
Add the following onSelect() method, which assigns the clicked hero
from the template to the component's selectedHero .
https://angular.io/tutorial/tour-of-heroes/toh-pt2 6/10
05/07/2023, 16:00 Angular - Display a selection list
src/app/heroes/heroes.component.ts (onSelect)
Skip to main content
selectedHero?: Hero;
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
<div *ngIf="selectedHero">
<h2>{{selectedHero.name | uppercase}} Details</h2>
<div>id: {{selectedHero.id}}</div>
<div>
<label for="hero-name">Hero name: </label>
<input id="hero-name"
[(ngModel)]="selectedHero.name" placeholder="name">
</div>
</div>
The hero details should only be displayed when a hero is selected. When a
component is created initially, there is no selected hero. Add the *ngIf
directive to the <div> that wraps the hero details. This directive tells
Angular to render the section only when the selectedHero is defined
after it has been selected by clicking on a hero.
https://angular.io/tutorial/tour-of-heroes/toh-pt2 7/10
05/07/2023, 16:00 Angular - Display a selection list
Angular's class binding can add and remove a CSS class conditionally.
When the current row hero is the same as the selectedHero , Angular
adds the selected CSS class. When the two heroes are different,
Angular removes the class.
https://angular.io/tutorial/tour-of-heroes/toh-pt2 8/10
05/07/2023, 16:00 Angular - Display a selection list
src/app/mock-heroes.ts src/app/heroes/heroes.component.ts
https://angular.io/tutorial/tour-of-heroes/toh-pt2 9/10
05/07/2023, 16:00 Angular - Display a selection list
Summary
The Tour of Heroes application displays a list of heroes with a detail
view.
The user can select a hero and see that hero's details.
You used *ngFor to display a list.
https://angular.io/tutorial/tour-of-heroes/toh-pt2 10/10