| @@ -1,2 +1,3 @@ | |||||
| <h1>{{title}}</h1> | <h1>{{title}}</h1> | ||||
| <app-heroes></app-heroes> | <app-heroes></app-heroes> | ||||
| <app-messages></app-messages> | |||||
| @@ -5,12 +5,14 @@ import { AppComponent } from './app.component'; | |||||
| import { HeroesComponent } from './heroes/heroes.component'; | import { HeroesComponent } from './heroes/heroes.component'; | ||||
| import {FormsModule} from '@angular/forms'; | import {FormsModule} from '@angular/forms'; | ||||
| 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, | ||||
| @@ -0,0 +1,12 @@ | |||||
| import { TestBed } from '@angular/core/testing'; | |||||
| import { HeroService } from './hero.service'; | |||||
| describe('HeroService', () => { | |||||
| beforeEach(() => TestBed.configureTestingModule({})); | |||||
| it('should be created', () => { | |||||
| const service: HeroService = TestBed.get(HeroService); | |||||
| expect(service).toBeTruthy(); | |||||
| }); | |||||
| }); | |||||
| @@ -0,0 +1,19 @@ | |||||
| import { Injectable } from '@angular/core'; | |||||
| import { Hero } from './hero'; | |||||
| import { HEROES } from './mock-heroes'; | |||||
| import { Observable, of } from 'rxjs'; | |||||
| import { MessageService } from './message.service'; | |||||
| @Injectable({ | |||||
| providedIn: 'root' | |||||
| }) | |||||
| export class HeroService { | |||||
| constructor(private messageService: MessageService) { } | |||||
| getHeroes(): Observable<Hero[]> { | |||||
| this.messageService.add('HeroService: fetched heroes') | |||||
| return of(HEROES); | |||||
| } | |||||
| } | |||||
| @@ -1,6 +1,7 @@ | |||||
| import {Component, OnInit} from '@angular/core'; | |||||
| import {Hero} from '../hero'; | |||||
| import {HEROES} from '../mock-heroes'; | |||||
| import { Component, OnInit } from '@angular/core'; | |||||
| import { Hero } from '../hero'; | |||||
| import { HEROES } from '../mock-heroes'; | |||||
| import { HeroService } from '../hero.service'; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-heroes', | selector: 'app-heroes', | ||||
| @@ -9,19 +10,20 @@ import {HEROES} from '../mock-heroes'; | |||||
| }) | }) | ||||
| export class HeroesComponent implements OnInit { | export class HeroesComponent implements OnInit { | ||||
| constructor() { | |||||
| constructor(private heroService: HeroService) { | |||||
| } | } | ||||
| hero: Hero = { | |||||
| id: 1, | |||||
| name: 'Windstorm' | |||||
| }; | |||||
| heroes = HEROES; | |||||
| heroes: Hero[]; | |||||
| selectedHero: Hero; | selectedHero: Hero; | ||||
| ngOnInit() { | ngOnInit() { | ||||
| this.getHeroes(); | |||||
| } | |||||
| getHeroes(): void { | |||||
| this.heroService.getHeroes() | |||||
| .subscribe(heroes => this.heroes = heroes); | |||||
| } | } | ||||
| onSelect(hero: Hero) { | onSelect(hero: Hero) { | ||||
| @@ -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(); | |||||
| }); | |||||
| }); | |||||
| @@ -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 = []; | |||||
| } | |||||
| } | |||||
| @@ -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; | |||||
| } | |||||
| @@ -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> | |||||
| @@ -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(); | |||||
| }); | |||||
| }); | |||||
| @@ -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() { | |||||
| } | |||||
| } | |||||