6: Get Data from a Server
[examples/angular-tour-of-heroes] / src / app / hero.service.ts
index 75a002b..6df713d 100644 (file)
@@ -13,6 +13,9 @@ import { catchError, tap } from 'rxjs/operators';
 export class HeroService {
 
   heroesUrl = 'api/heroes';  // URL to web api
+  httpOptions = {
+    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
+  };
 
   constructor(
     private http: HttpClient,
@@ -38,6 +41,14 @@ export class HeroService {
     );
   }
 
+  /** PUT: update the hero on the server */
+  updateHero(hero: Hero): Observable<any> {
+    return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(
+      tap(_ => this.log(`updated hero id=${hero.id}`)),
+      catchError(this.handleError<any>('updateHero'))
+    );
+  }
+
   /**
    * Handle Http operation that failed.
    * Let the app continue.