Skip to content

Commit 50a178a

Browse files
committed
Adding service & refactoring app.component.ts (keep following tutorial)
1 parent c909850 commit 50a178a

File tree

6 files changed

+81
-34
lines changed

6 files changed

+81
-34
lines changed

src/app/app.component.ts

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,43 @@
11
import { Component } from '@angular/core';
2-
3-
4-
export class Hero {
5-
id: number;
6-
name: string;
7-
}
8-
9-
const HEROES: Hero[] = [
10-
{ id: 11, name: 'Mr. Nice' },
11-
{ id: 12, name: 'Narco' },
12-
{ id: 13, name: 'Bombasto' },
13-
{ id: 14, name: 'Celeritas' },
14-
{ id: 15, name: 'Magneta' },
15-
{ id: 16, name: 'RubberMan' },
16-
{ id: 17, name: 'Dynama' },
17-
{ id: 18, name: 'Dr IQ' },
18-
{ id: 19, name: 'Magma' },
19-
{ id: 20, name: 'Tornado' }
20-
];
21-
22-
2+
import { Hero } from './hero';
3+
import { HeroService } from './hero.service';
4+
import { OnInit } from '@angular/core';
235

246
@Component({
257
selector: 'app-root',
268
template: `
279
<h1>{{title}}</h1>
2810
<ul class="heroes">
29-
<li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
11+
<li *ngFor="let hero of heroes"
12+
(click)="onSelect(hero)"
13+
[class.selected]="hero === selectedHero">
3014
<span class="badge">{{hero.id}}</span> {{hero.name}}
3115
</li>
3216
</ul>
33-
<div *ngIf="selectedHero">
34-
<h2>{{selectedHero.name}} details!</h2>
35-
<div><label>id: </label>{{selectedHero.id}}</div>
36-
<div>
37-
<label>name: </label>
38-
<input [(ngModel)]="selectedHero.name" placeholder="name"/>
39-
</div>
40-
</div>
17+
<hero-detail [hero]="selectedHero"></hero-detail>
4118
`,
42-
styleUrls: ['./app.component.css']
19+
styleUrls: ['./app.component.css'],
20+
providers: [HeroService]
4321
})
4422

45-
export class AppComponent {
23+
24+
export class AppComponent implements OnInit {
4625
title = 'Tour of Heroes';
47-
heroes = HEROES;
26+
heroes: Hero[];
4827
selectedHero: Hero;
4928

29+
constructor(private heroService: HeroService) { }
30+
5031
onSelect(hero: Hero): void {
5132
this.selectedHero = hero;
5233
}
34+
35+
ngOnInit(): void {
36+
this.getHeroes();
37+
}
38+
39+
getHeroes(): void {
40+
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
41+
// this.heroService.getHeroesSlowly().then(heroes => this.heroes = heroes);
42+
}
5343
}

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33
import { FormsModule } from '@angular/forms';
4+
import { HeroDetailComponent } from './hero-detail.component';
45

56
import { AppComponent } from './app.component';
67

78
@NgModule({
89
declarations: [
9-
AppComponent
10+
AppComponent,
11+
HeroDetailComponent
1012
],
1113
imports: [
1214
BrowserModule,

src/app/hero-detail.component.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Component, Input } from '@angular/core';
2+
import { Hero } from './hero';
3+
4+
@Component({
5+
selector: 'hero-detail',
6+
template: `
7+
<div *ngIf="hero">
8+
<h2>{{hero.name}} details!</h2>
9+
<div><label>id: </label>{{hero.id}}</div>
10+
<div>
11+
<label>name: </label>
12+
<input [(ngModel)]="hero.name" placeholder="name"/>
13+
</div>
14+
</div>
15+
`
16+
})
17+
export class HeroDetailComponent {
18+
@Input() hero: Hero;
19+
}

src/app/hero.service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable } from '@angular/core';
2+
import { HEROES } from './mock-heroes';
3+
import { Hero } from './hero';
4+
5+
@Injectable()
6+
export class HeroService {
7+
8+
getHeroes(): Promise<Hero[]> {
9+
return Promise.resolve(HEROES);
10+
}
11+
12+
getHeroesSlowly(): Promise<Hero[]> {
13+
return new Promise(resolve => {
14+
// Simulate server latency with 2 second delay
15+
setTimeout(() => resolve(this.getHeroes()), 2000);
16+
});
17+
}
18+
}

src/app/hero.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class Hero {
2+
id: number;
3+
name: string;
4+
}

src/app/mock-heroes.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Hero } from './hero';
2+
3+
export const HEROES: Hero[] = [
4+
{ id: 11, name: 'Mr. Nice' },
5+
{ id: 12, name: 'Narco' },
6+
{ id: 13, name: 'Bombasto' },
7+
{ id: 14, name: 'Celeritas' },
8+
{ id: 15, name: 'Magneta' },
9+
{ id: 16, name: 'RubberMan' },
10+
{ id: 17, name: 'Dynama' },
11+
{ id: 18, name: 'Dr IQ' },
12+
{ id: 19, name: 'Magma' },
13+
{ id: 20, name: 'Tornado' }
14+
];

0 commit comments

Comments
 (0)