Angular Tour of Heroes: reprise du chapitre 4 (Services)

This commit is contained in:
Olivier DOSSMANN 2019-10-25 15:56:29 +02:00
parent c735c39693
commit 3f1898c529
9 changed files with 120 additions and 2 deletions

View File

@ -1,2 +1,3 @@
<h1>{{title}}</h1> <h1>{{title}}</h1>
<app-heroes></app-heroes> <app-heroes></app-heroes>
<app-messages></app-messages>

View File

@ -5,12 +5,14 @@ import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { HeroesComponent } from './heroes/heroes.component'; import { HeroesComponent } from './heroes/heroes.component';
import { HeroDetailComponent } from './hero-detail/hero-detail.component'; import { HeroDetailComponent } from './hero-detail/hero-detail.component';
import { MessagesComponent } from './messages/messages.component';
@NgModule({ @NgModule({
declarations: [ declarations: [
AppComponent, AppComponent,
HeroesComponent, HeroesComponent,
HeroDetailComponent HeroDetailComponent,
MessagesComponent
], ],
imports: [ imports: [
BrowserModule, BrowserModule,

View File

@ -2,15 +2,18 @@ import { Injectable } from '@angular/core';
import { Hero } from './hero'; import { Hero } from './hero';
import { HEROES } from './mock-heroes'; import { HEROES } from './mock-heroes';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { MessageService } from './message.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class HeroService { export class HeroService {
constructor() { } constructor(private messageService: MessageService) { }
getHeroes(): Observable<Hero[]> { getHeroes(): Observable<Hero[]> {
// TODO: send the message _after_ fetching the heroes
this.messageService.add('HeroService: fetched heroes.')
return of(HEROES); return of(HEROES);
} }
} }

View File

@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { MessageService } from './message.service';
describe('MessageService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: MessageService = TestBed.get(MessageService);
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,16 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MessageService {
messages: string[] = [];
add(message: string) {
this.messages.push(message);
}
clear() {
this.messages = [];
}
}

View File

@ -0,0 +1,35 @@
/* MessagesComponent's private CSS styles */
h2 {
color: red;
font-family: Arial, Helvetica, sans-serif;
font-weight: lighter;
}
body {
margin: 2em;
}
body, input[text], button {
color: crimson;
font-family: Cambria, Georgia;
}
button.clear {
font-family: Arial;
background-color: #eee;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
cursor: hand;
}
button:hover {
background-color: #cfd8dc;
}
button:disabled {
background-color: #eee;
color: #aaa;
cursor: auto;
}
button.clear {
color: #333;
margin-bottom: 12px;
}

View File

@ -0,0 +1,8 @@
<div *ngIf="messageService.messages.length">
<h2>Messages</h2>
<button class="clear"
(click)="messageService.clear()">clear</button>
<div *ngFor='let message of messageService.messages'> {{message}} </div>
</div>

View File

@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MessagesComponent } from './messages.component';
describe('MessagesComponent', () => {
let component: MessagesComponent;
let fixture: ComponentFixture<MessagesComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MessagesComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MessagesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,16 @@
import { Component, OnInit } from '@angular/core';
import { MessageService } from '../message.service';
@Component({
selector: 'app-messages',
templateUrl: './messages.component.html',
styleUrls: ['./messages.component.css']
})
export class MessagesComponent implements OnInit {
constructor(public messageService: MessageService) { }
ngOnInit() {
}
}