Completed Angular Services Chapter
This commit is contained in:
parent
32de0c2c92
commit
3ce4a46a76
11 changed files with 149 additions and 5 deletions
|
@ -1,2 +1,3 @@
|
|||
<h1>{{ title }}</h1>
|
||||
<app-heroes></app-heroes>
|
||||
<app-messages></app-messages>
|
|
@ -6,12 +6,14 @@ import { AppRoutingModule } from './app-routing.module';
|
|||
import { AppComponent } from './app.component';
|
||||
import { HeroesComponent } from './heroes/heroes.component';
|
||||
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
|
||||
import { MessagesComponent } from './messages/messages.component';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AppComponent,
|
||||
HeroesComponent,
|
||||
HeroDetailComponent
|
||||
HeroDetailComponent,
|
||||
MessagesComponent
|
||||
],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
|
|
16
src/app/hero.service.spec.ts
Normal file
16
src/app/hero.service.spec.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { HeroService } from './hero.service';
|
||||
|
||||
describe('HeroService', () => {
|
||||
let service: HeroService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(HeroService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
19
src/app/hero.service.ts
Normal file
19
src/app/hero.service.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { Hero } from './hero';
|
||||
import { MessageService } from './messages.service';
|
||||
import { HEROES } from './mock-heroes';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class HeroService {
|
||||
|
||||
constructor(private messageService: MessageService) { }
|
||||
|
||||
getHeroes(): Observable<Hero[]> {
|
||||
const heroes = of(HEROES)
|
||||
this.messageService.add('HeroService: Heroes fetched!')
|
||||
return heroes
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
import { Hero } from '../hero';
|
||||
import { HEROES } from '../mock-heroes';
|
||||
import { HeroService } from '../hero.service';
|
||||
import { MessageService } from '../messages.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-heroes',
|
||||
|
@ -9,16 +10,23 @@ import { HEROES } from '../mock-heroes';
|
|||
})
|
||||
export class HeroesComponent implements OnInit {
|
||||
|
||||
constructor() { }
|
||||
constructor(private heroService: HeroService, private messageService: MessageService) { }
|
||||
|
||||
selectedHero?: Hero
|
||||
|
||||
onSelect(hero: Hero): void {
|
||||
this.selectedHero = hero
|
||||
this.messageService.add(`Hero Component: Selected hero id=${hero.id}`)
|
||||
}
|
||||
|
||||
heroes = HEROES
|
||||
heroes: Hero[] = []
|
||||
|
||||
ngOnInit(): void {
|
||||
this.getHeroes()
|
||||
}
|
||||
|
||||
getHeroes(): void {
|
||||
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
16
src/app/messages.service.spec.ts
Normal file
16
src/app/messages.service.spec.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { MessageService } from './messages.service';
|
||||
|
||||
describe('MessagesService', () => {
|
||||
let service: MessageService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(MessageService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
18
src/app/messages.service.ts
Normal file
18
src/app/messages.service.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MessageService {
|
||||
messages: string[] = []
|
||||
|
||||
add(message: string) {
|
||||
this.messages.push(message)
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.messages = []
|
||||
}
|
||||
|
||||
constructor() { }
|
||||
}
|
6
src/app/messages/messages.component.html
Normal file
6
src/app/messages/messages.component.html
Normal file
|
@ -0,0 +1,6 @@
|
|||
<div *ngIf="messageService.messages.length">
|
||||
|
||||
<h2>Messages</h2>
|
||||
<button type="button" class="clear" (click)="messageService.clear()">Clear Messages</button>
|
||||
<div *ngFor="let message of messageService.messages">{{message}}</div>
|
||||
</div>
|
19
src/app/messages/messages.component.scss
Normal file
19
src/app/messages/messages.component.scss
Normal file
|
@ -0,0 +1,19 @@
|
|||
/* MessagesComponent's private CSS styles */
|
||||
h2 {
|
||||
color: #A80000;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
font-weight: lighter;
|
||||
}
|
||||
|
||||
.clear {
|
||||
color: #333;
|
||||
background-color: #eee;
|
||||
margin-bottom: 12px;
|
||||
padding: 1rem;
|
||||
border-radius: 4px;
|
||||
font-size: 1rem;
|
||||
}
|
||||
.clear:hover {
|
||||
color: white;
|
||||
background-color: #42545C;
|
||||
}
|
23
src/app/messages/messages.component.spec.ts
Normal file
23
src/app/messages/messages.component.spec.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { MessagesComponent } from './messages.component';
|
||||
|
||||
describe('MessagesComponent', () => {
|
||||
let component: MessagesComponent;
|
||||
let fixture: ComponentFixture<MessagesComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
declarations: [ MessagesComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(MessagesComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
16
src/app/messages/messages.component.ts
Normal file
16
src/app/messages/messages.component.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
import { MessageService } from '../messages.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-messages',
|
||||
templateUrl: './messages.component.html',
|
||||
styleUrls: ['./messages.component.scss']
|
||||
})
|
||||
export class MessagesComponent implements OnInit {
|
||||
|
||||
constructor(public messageService: MessageService) { }
|
||||
|
||||
ngOnInit(): void {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue