1.5 Angular - Create A Feature Component
1.5 Angular - Create A Feature Component
mode_edit
Create a feature
component
At the moment, the HeroesComponent displays both the list of heroes
and the selected hero's details.
The first step is to move the hero details into a separate, reusable
HeroDetailComponent and end up with:
For the sample application that this page describes, see the live
https://angular.io/tutorial/tour-of-heroes/toh-pt3 1/7
05/07/2023, 16:00 Angular - Create a feature component
https://angular.io/tutorial/tour-of-heroes/toh-pt3 2/7
05/07/2023, 16:00 Angular - Create a feature component
src/app/hero-detail/hero-detail.component.html
Skip to main content
<div *ngIf="hero">
</div>
Open the HeroDetailComponent class file and import the Hero symbol.
<app-hero-detail [hero]="selectedHero"></app-hero-
detail>
symbol.
https://angular.io/tutorial/tour-of-heroes/toh-pt3 3/7
05/07/2023, 16:00 Angular - Create a feature component
src/app/hero-detail/hero-detail.component.ts
displays it.
list.
its template.
<app-hero-detail [hero]="selectedHero"></app-hero-
detail>
Now when the user clicks a hero in the list, the selectedHero changes.
When the selectedHero changes, the property binding updates hero
heroes.component.html
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes">
<button [class.selected]="hero === selectedHero"
type="button" (click)="onSelect(hero)">
<span class="badge">{{hero.id}}</span>
<span class="name">{{hero.name}}</span>
</button>
</li>
</ul>
<app-hero-detail [hero]="selectedHero"></app-hero-
detail>
The browser refreshes and the application starts working again as it did
before.
https://angular.io/tutorial/tour-of-heroes/toh-pt3 5/7
05/07/2023, 16:00 Angular - Create a feature component
What changed?
As before, whenever a user clicks on a hero name, the hero detail appears
src/app/hero-detail/hero-detail.component.ts src/app/hero-detai
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent {
https://angular.io/tutorial/tour-of-heroes/toh-pt3 6/7
05/07/2023, 16:00 Angular - Create a feature component
Summary
You created a separate, reusable HeroDetailComponent .
https://angular.io/tutorial/tour-of-heroes/toh-pt3 7/7