Skip to content

Commit 379bc5d

Browse files
committed
Adding messages by message-service
1 parent 8d63dc9 commit 379bc5d

10 files changed

Lines changed: 91 additions & 5 deletions

src/app/app.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<h1>{{ title }}</h1>
2-
<app-heroes></app-heroes>
2+
<app-heroes></app-heroes>
3+
<app-messages></app-messages>

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { BrowserModule } from '@angular/platform-browser';
55
import { AppComponent } from './app.component';
66
import { HeroesComponent } from './heroes/heroes.component';
77
import { 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,

src/app/hero.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import { Injectable } from '@angular/core';
22
import { Observable, of } from 'rxjs';
33
import { HEROES } from './data/mock-heroes';
44
import { Hero } from './heroes/hero';
5+
import { MessageService } from './message.service';
56

67
@Injectable({
78
providedIn: 'root'
89
})
910
export 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
}

src/app/heroes/heroes.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { HeroService } from '../hero.service';
3+
import { MessageService } from '../message.service';
34
import { 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
}

src/app/message.service.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
});

src/app/message.service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

src/app/messages/messages.component.css

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

0 commit comments

Comments
 (0)