Completed Angular Services Chapter

This commit is contained in:
nikurasu 2022-09-24 19:04:42 +02:00
parent 32de0c2c92
commit 3ce4a46a76
11 changed files with 149 additions and 5 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

@ -6,12 +6,14 @@ import { AppRoutingModule } from './app-routing.module';
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

@ -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
View 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
}
}

View file

@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero'; import { Hero } from '../hero';
import { HEROES } from '../mock-heroes'; import { HeroService } from '../hero.service';
import { MessageService } from '../messages.service';
@Component({ @Component({
selector: 'app-heroes', selector: 'app-heroes',
@ -9,16 +10,23 @@ import { HEROES } from '../mock-heroes';
}) })
export class HeroesComponent implements OnInit { export class HeroesComponent implements OnInit {
constructor() { } constructor(private heroService: HeroService, private messageService: MessageService) { }
selectedHero?: Hero selectedHero?: Hero
onSelect(hero: Hero): void { onSelect(hero: Hero): void {
this.selectedHero = hero this.selectedHero = hero
this.messageService.add(`Hero Component: Selected hero id=${hero.id}`)
} }
heroes = HEROES heroes: Hero[] = []
ngOnInit(): void { ngOnInit(): void {
this.getHeroes()
}
getHeroes(): void {
this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes)
} }
} }

View 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();
});
});

View 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() { }
}

View 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>

View 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;
}

View 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();
});
});

View 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 {
}
}