2: Display a List
authorKai Moritz <kai@juplo.de>
Sat, 2 May 2020 10:38:51 +0000 (12:38 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 2 May 2020 17:17:18 +0000 (19:17 +0200)
b) Master/Detail

src/app/heroes/heroes.component.html
src/app/heroes/heroes.component.ts

index dd417cf..cd87f96 100644 (file)
@@ -1,12 +1,14 @@
 <h2>My Heroes</h2>
 <ul class="heroes">
-  <li *ngFor="let hero of heroes">
+  <li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
     <span class="badge">{{hero.id}}</span> {{hero.name}}
   </li>
 </ul>
-<h2>{{hero.name | uppercase}} Details</h2>
-<div><span>id: </span>{{hero.id}}</div>
-<div>
-  <label>name: </label>
-  <input [(ngModel)]="hero.name" placeholder="Type a name..." />
+<div *ngIf="selectedHero">
+  <h2>{{selectedHero.name | uppercase}} Details</h2>
+  <div><span>id: </span>{{selectedHero.id}}</div>
+  <div>
+    <label>name: </label>
+    <input [(ngModel)]="selectedHero.name" placeholder="Type a name..." />
+  </div>
 </div>
index 2bb7a69..a09ff13 100644 (file)
@@ -11,10 +11,11 @@ export class HeroesComponent implements OnInit {
 
   heroes = HEROES;
 
-  hero: Hero = {
-    id: 1,
-    name: 'Windstorm',
-  };
+  selectedHero : Hero;
+
+  onSelect(hero : Hero): void {
+    this.selectedHero = hero;
+  }
 
   constructor() { }