Skip to content

Commit 81874be

Browse files
committed
模拟web API 利用HTTP服务实现更新添加删除英雄
1 parent c234571 commit 81874be

18 files changed

+226
-17
lines changed

src/app/app.module.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/app.module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/app.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { NgModule } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
33
import { FormsModule } from '@angular/forms';
4-
import { RouterModule } from '@angular/router';
4+
import { HttpModule } from '@angular/http';
5+
6+
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
7+
import { InMemoryDataService } from './in-memory-data.service';
58

69
import { AppComponent } from './app.component';
710
import { HeroDetailComponent } from './hero-detail.component';
@@ -16,6 +19,8 @@ import { AppRoutingModule } from './app-routing.module';
1619
BrowserModule,
1720
FormsModule,
1821
AppRoutingModule,
22+
HttpModule,
23+
InMemoryWebApiModule.forRoot(InMemoryDataService),
1924
],
2025
declarations: [
2126
AppComponent,

src/app/hero-detail.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ <h2>{{hero.name}} details!</h2>
66
<input [(ngModel)]="hero.name" placeholder="name">
77
</div>
88
<button (click)="goBack()">Back</button>
9+
<button (click)="save()">Save</button>
910
</div>

src/app/hero-detail.component.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/hero-detail.component.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/hero-detail.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ export class HeroDetailComponent implements OnInit {
2929
goBack(): void{
3030
this.location.back();
3131
}
32+
33+
save(): void{
34+
this.heroService.update(this.hero)
35+
.then(() => this.goBack());
36+
}
3237
}

src/app/hero.service.js

Lines changed: 45 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/hero.service.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/hero.service.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
import { Injectable } from '@angular/core';
2+
import { Headers, Http } from '@angular/http';
3+
4+
import 'rxjs/add/operator/toPromise';
5+
26
import { Hero } from './hero';
3-
import { HEROES } from './mock-heroes';
47

58
@Injectable()
69
export class HeroService{
7-
getHeroes(): Promise<Hero[]>{
8-
return Promise.resolve(HEROES);
10+
private heroesUrl = 'api/heroes'; // URL to web api
11+
12+
constructor(private http: Http) { }
13+
14+
getHeroes(): Promise<Hero[]> {
15+
return this.http.get(this.heroesUrl)
16+
.toPromise()
17+
.then(response => response.json().data as Hero[])
18+
.catch(this.handleError);
919
}
1020

21+
private handleError(error: any): Promise<any> {
22+
console.error('An error occurred', error); // for demo purposes only
23+
return Promise.reject(error.message || error);
24+
}
25+
26+
1127
getHero(id: number): Promise<Hero>{
12-
return this.getHeroes()
13-
.then(heroes => heroes.find(hero => hero.id === id));
28+
const url = `${this.heroesUrl}/${id}`;
29+
return this.http.get(url)
30+
.toPromise()
31+
.then(response => response.json().data as Hero)
32+
.catch(this.handleError);
1433
}
1534

1635
getHeroesSlowly(): Promise<Hero[]> {
@@ -19,4 +38,30 @@ export class HeroService{
1938
setTimeout(() => resolve(this.getHeroes()), 2000);
2039
});
2140
}
41+
42+
private headers = new Headers({'Content-Type': 'application/json'});
43+
update(hero: Hero): Promise<Hero>{
44+
const url = `${this.heroesUrl}/${hero.id}`;
45+
return this.http.put(url, JSON.stringify(hero), {headers: this.headers})
46+
.toPromise()
47+
.then(() => hero)
48+
.catch(this.handleError);
49+
}
50+
51+
create(name: string): Promise<Hero>{
52+
return this.http
53+
.post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers})
54+
.toPromise()
55+
.then(res => res.json().data as Hero)
56+
.catch(this.handleError);
57+
}
58+
59+
delete(id: number): Promise<void>{
60+
const url = `${this.heroesUrl}/${id}`;
61+
return this.http.delete(url, {headers: this.headers})
62+
.toPromise()
63+
.then(() => null)
64+
.catch(this.handleError);
65+
}
66+
2267
}

0 commit comments

Comments
 (0)