6: Get Data from a Server
[examples/angular-tour-of-heroes] / src / app / heroes / heroes.component.ts
index eaa069b..38ab888 100644 (file)
@@ -25,4 +25,13 @@ export class HeroesComponent implements OnInit {
         .getHeroes()
         .subscribe(heroes => this.heroes = heroes);
   }
+
+  add(name: string): void {
+    name = name.trim();
+    if (!name) { return; }
+    this.heroService.addHero({ name } as Hero)
+      .subscribe(hero => {
+        this.heroes.push(hero);
+      });
+  }
 }