File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11< h1 > {{ title }}</ h1 >
2- < app-heroes > </ app-heroes >
2+ < app-heroes > </ app-heroes >
3+ < app-messages > </ app-messages >
Original file line number Diff line number Diff line change @@ -5,12 +5,14 @@ import { BrowserModule } from '@angular/platform-browser';
55import { AppComponent } from './app.component' ;
66import { HeroesComponent } from './heroes/heroes.component' ;
77import { HeroDetailComponent } from './hero-detail/hero-detail.component' ;
8+ import { MessagesComponent } from './messages/messages.component' ;
89
910@NgModule ( {
1011 declarations : [
1112 AppComponent ,
1213 HeroesComponent ,
13- HeroDetailComponent
14+ HeroDetailComponent ,
15+ MessagesComponent
1416 ] ,
1517 imports : [
1618 BrowserModule ,
Original file line number Diff line number Diff line change @@ -2,17 +2,18 @@ import { Injectable } from '@angular/core';
22import { Observable , of } from 'rxjs' ;
33import { HEROES } from './data/mock-heroes' ;
44import { Hero } from './heroes/hero' ;
5+ import { MessageService } from './message.service' ;
56
67@Injectable ( {
78 providedIn : 'root'
89} )
910export class HeroService {
1011
11- constructor ( ) { }
12+ constructor ( private messageService : MessageService ) { }
1213
1314 getHeroes ( ) : Observable < Hero [ ] > {
1415 const heroes = of ( HEROES )
16+ this . messageService . add ( 'HeroService: fetched heroes' )
1517 return heroes
16-
1718 }
1819}
Original file line number Diff line number Diff line change 11import { Component , OnInit } from '@angular/core' ;
22import { HeroService } from '../hero.service' ;
3+ import { MessageService } from '../message.service' ;
34import { Hero } from './hero' ;
45
56@Component ( {
@@ -11,7 +12,7 @@ export class HeroesComponent implements OnInit {
1112 heroes ?: Hero [ ] = [ ]
1213 selectedHero ?: Hero
1314
14- constructor ( private heroService : HeroService ) { }
15+ constructor ( private heroService : HeroService , private messageService : MessageService ) { }
1516
1617 getHeroes ( ) : void {
1718 this . heroService . getHeroes ( ) . subscribe ( heroes => this . heroes = heroes )
@@ -23,5 +24,6 @@ export class HeroesComponent implements OnInit {
2324
2425 onSelect ( hero : Hero ) : void {
2526 this . selectedHero = hero
27+ this . messageService . add ( `HeroesComponent: Selected hero id=${ hero . id } ` )
2628 }
2729}
Original file line number Diff line number Diff line change 1+ import { TestBed } from '@angular/core/testing' ;
2+
3+ import { MessageService } from './message.service' ;
4+
5+ describe ( 'MessageService' , ( ) => {
6+ let service : MessageService ;
7+
8+ beforeEach ( ( ) => {
9+ TestBed . configureTestingModule ( { } ) ;
10+ service = TestBed . inject ( MessageService ) ;
11+ } ) ;
12+
13+ it ( 'should be created' , ( ) => {
14+ expect ( service ) . toBeTruthy ( ) ;
15+ } ) ;
16+ } ) ;
Original file line number Diff line number Diff line change 1+ import { Injectable } from '@angular/core' ;
2+
3+ @Injectable ( {
4+ providedIn : 'root'
5+ } )
6+ export class MessageService {
7+ messages : string [ ] = [ ]
8+
9+ add ( message : string ) : void {
10+ this . messages . push ( message )
11+ }
12+
13+ clear ( ) : void {
14+ this . messages = [ ]
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ < div *ngIf ="messageService.messages.length ">
2+ < h2 > Messages</ h2 >
3+ < button type ="button " class ="clear " (click) ="messageService.clear() ">
4+ Clear Message
5+ </ button >
6+ < div *ngFor ='let message of messageService.messages '> {{message}} </ div >
7+ </ div >
Original file line number Diff line number Diff line change 1+ import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2+
3+ import { MessagesComponent } from './messages.component' ;
4+
5+ describe ( 'MessagesComponent' , ( ) => {
6+ let component : MessagesComponent ;
7+ let fixture : ComponentFixture < MessagesComponent > ;
8+
9+ beforeEach ( async ( ) => {
10+ await TestBed . configureTestingModule ( {
11+ declarations : [ MessagesComponent ]
12+ } )
13+ . compileComponents ( ) ;
14+ } ) ;
15+
16+ beforeEach ( ( ) => {
17+ fixture = TestBed . createComponent ( MessagesComponent ) ;
18+ component = fixture . componentInstance ;
19+ fixture . detectChanges ( ) ;
20+ } ) ;
21+
22+ it ( 'should create' , ( ) => {
23+ expect ( component ) . toBeTruthy ( ) ;
24+ } ) ;
25+ } ) ;
Original file line number Diff line number Diff line change 1+ import { Component , OnInit } from '@angular/core' ;
2+ import { MessageService } from '../message.service' ;
3+
4+ @Component ( {
5+ selector : 'app-messages' ,
6+ templateUrl : './messages.component.html' ,
7+ styleUrls : [ './messages.component.css' ]
8+ } )
9+ export class MessagesComponent implements OnInit {
10+
11+ constructor ( public messageService : MessageService ) { }
12+
13+ ngOnInit ( ) : void {
14+ }
15+
16+ }
You can’t perform that action at this time.
0 commit comments