4: Add Services
[examples/angular-tour-of-heroes] / src / app / heroes / heroes.component.ts
index 94f4266..d8ac642 100644 (file)
@@ -1,6 +1,7 @@
 import { Component, OnInit } from '@angular/core';
 import { Hero } from '../hero';
 import { HeroService } from '../hero.service';
+import { MessageService } from '../message.service';
 
 @Component({
   selector: 'app-heroes',
@@ -15,15 +16,20 @@ export class HeroesComponent implements OnInit {
 
   onSelect(hero : Hero): void {
     this.selectedHero = hero;
+    this.messageService.add(`HeroService: Selected hero id=${hero.id}`);
   }
 
-  constructor(private heroService : HeroService) { }
+  constructor(
+      private heroService : HeroService,
+      private messageService : MessageService) { }
 
   ngOnInit() {
     this.getHeroes();
   }
 
   getHeroes() : void {
-    this.heroes = this.heroService.getHeroes();
+    this.heroService
+        .getHeroes()
+        .subscribe(heroes => this.heroes = heroes);
   }
 }