{ "id": "tutorial/toh-pt6", "title": "Get data from a server", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Get data from a serverlink

\n

In this tutorial, you'll add the following data persistence features with help from\nAngular's HttpClient.

\n\n
\n

For the sample application that this page describes, see the .

\n
\n

Enable HTTP serviceslink

\n

HttpClient is Angular's mechanism for communicating with a remote server over HTTP.

\n

Make HttpClient available everywhere in the application in two steps. First, add it to the root AppModule by importing it:

\n\nimport { HttpClientModule } from '@angular/common/http';\n\n\n

Next, still in the AppModule, add HttpClient to the imports array:

\n\n@NgModule({\n imports: [\n HttpClientModule,\n ],\n})\n\n\n

Simulate a data serverlink

\n

This tutorial sample mimics communication with a remote data server by using the\nIn-memory Web API module.

\n

After installing the module, the application will make requests to and receive responses from the HttpClient\nwithout knowing that the In-memory Web API is intercepting those requests,\napplying them to an in-memory data store, and returning simulated responses.

\n

By using the In-memory Web API, you won't have to set up a server to learn about HttpClient.

\n
\n

Important: the In-memory Web API module has nothing to do with HTTP in Angular.

\n

If you're just reading this tutorial to learn about HttpClient, you can skip over this step.\nIf you're coding along with this tutorial, stay here and add the In-memory Web API now.

\n
\n

Install the In-memory Web API package from npm with the following command:

\n\n npm install angular-in-memory-web-api --save\n\n

In the AppModule, import the HttpClientInMemoryWebApiModule and the InMemoryDataService class,\nwhich you will create in a moment.

\n\nimport { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';\nimport { InMemoryDataService } from './in-memory-data.service';\n\n\n

After the HttpClientModule, add the HttpClientInMemoryWebApiModule\nto the AppModule imports array and configure it with the InMemoryDataService.

\n\nHttpClientModule,\n\n// The HttpClientInMemoryWebApiModule module intercepts HTTP requests\n// and returns simulated server responses.\n// Remove it when a real server is ready to receive requests.\nHttpClientInMemoryWebApiModule.forRoot(\n InMemoryDataService, { dataEncapsulation: false }\n)\n\n\n

The forRoot() configuration method takes an InMemoryDataService class\nthat primes the in-memory database.

\n

Generate the class src/app/in-memory-data.service.ts with the following command:

\n\n ng generate service InMemoryData\n\n

Replace the default contents of in-memory-data.service.ts with the following:

\n\nimport { Injectable } from '@angular/core';\nimport { InMemoryDbService } from 'angular-in-memory-web-api';\nimport { Hero } from './hero';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class InMemoryDataService implements InMemoryDbService {\n createDb() {\n const heroes = [\n { id: 11, name: 'Dr Nice' },\n { id: 12, name: 'Narco' },\n { id: 13, name: 'Bombasto' },\n { id: 14, name: 'Celeritas' },\n { id: 15, name: 'Magneta' },\n { id: 16, name: 'RubberMan' },\n { id: 17, name: 'Dynama' },\n { id: 18, name: 'Dr IQ' },\n { id: 19, name: 'Magma' },\n { id: 20, name: 'Tornado' }\n ];\n return {heroes};\n }\n\n // Overrides the genId method to ensure that a hero always has an id.\n // If the heroes array is empty,\n // the method below returns the initial number (11).\n // if the heroes array is not empty, the method below returns the highest\n // hero id + 1.\n genId(heroes: Hero[]): number {\n return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11;\n }\n}\n\n\n\n

The in-memory-data.service.ts file will take over the function of mock-heroes.ts.\nHowever, don't delete mock-heroes.ts yet, as you still need it for a few more steps of this tutorial.

\n

When the server is ready, you'll detach the In-memory Web API, and the app's requests will go through to the server.

\n\n

Heroes and HTTPlink

\n

In the HeroService, import HttpClient and HttpHeaders:

\n\nimport { HttpClient, HttpHeaders } from '@angular/common/http';\n\n\n

Still in the HeroService, inject HttpClient into the constructor in a private property called http.

\n\nconstructor(\n private http: HttpClient,\n private messageService: MessageService) { }\n\n\n

Notice that you keep injecting the MessageService but since you'll call it so frequently, wrap it in a private log() method:

\n\n/** Log a HeroService message with the MessageService */\nprivate log(message: string) {\n this.messageService.add(`HeroService: ${message}`);\n}\n\n\n

Define the heroesUrl of the form :base/:collectionName with the address of the heroes resource on the server.\nHere base is the resource to which requests are made,\nand collectionName is the heroes data object in the in-memory-data-service.ts.

\n\nprivate heroesUrl = 'api/heroes'; // URL to web api\n\n\n

Get heroes with HttpClientlink

\n

The current HeroService.getHeroes()\nuses the RxJS of() function to return an array of mock heroes\nas an Observable<Hero[]>.

\n\ngetHeroes(): Observable<Hero[]> {\n const heroes = of(HEROES);\n return heroes;\n}\n\n\n

Convert that method to use HttpClient as follows:

\n\n/** GET heroes from the server */\ngetHeroes(): Observable<Hero[]> {\n return this.http.get<Hero[]>(this.heroesUrl)\n}\n\n\n

Refresh the browser. The hero data should successfully load from the\nmock server.

\n

You've swapped of() for http.get() and the application keeps working without any other changes\nbecause both functions return an Observable<Hero[]>.

\n

HttpClient methods return one valuelink

\n

All HttpClient methods return an RxJS Observable of something.

\n

HTTP is a request/response protocol.\nYou make a request, it returns a single response.

\n

In general, an observable can return multiple values over time.\nAn observable from HttpClient always emits a single value and then completes, never to emit again.

\n

This particular HttpClient.get() call returns an Observable<Hero[]>; that is, \"an observable of hero arrays\". In practice, it will only return a single hero array.

\n

HttpClient.get() returns response datalink

\n

HttpClient.get() returns the body of the response as an untyped JSON object by default.\nApplying the optional type specifier, <Hero[]> , adds TypeScript capabilities, which reduce errors during compile time.

\n

The server's data API determines the shape of the JSON data.\nThe Tour of Heroes data API returns the hero data as an array.

\n
\n

Other APIs may bury the data that you want within an object.\nYou might have to dig that data out by processing the Observable result\nwith the RxJS map() operator.

\n

Although not discussed here, there's an example of map() in the getHeroNo404()\nmethod included in the sample source code.

\n
\n

Error handlinglink

\n

Things go wrong, especially when you're getting data from a remote server.\nThe HeroService.getHeroes() method should catch errors and do something appropriate.

\n

To catch errors, you \"pipe\" the observable result from http.get() through an RxJS catchError() operator.

\n

Import the catchError symbol from rxjs/operators, along with some other operators you'll need later.

\n\nimport { catchError, map, tap } from 'rxjs/operators';\n\n\n

Now extend the observable result with the pipe() method and\ngive it a catchError() operator.

\n\ngetHeroes(): Observable<Hero[]> {\n return this.http.get<Hero[]>(this.heroesUrl)\n .pipe(\n catchError(this.handleError<Hero[]>('getHeroes', []))\n );\n}\n\n\n

The catchError() operator intercepts an Observable that failed.\nThe operator then passes the error to the error handling function.

\n

The following handleError() method reports the error and then returns an\ninnocuous result so that the application keeps working.

\n

handleErrorlink

\n

The following handleError() will be shared by many HeroService methods\nso it's generalized to meet their different needs.

\n

Instead of handling the error directly, it returns an error handler function to catchError that it\nhas configured with both the name of the operation that failed and a safe return value.

\n\n/**\n * Handle Http operation that failed.\n * Let the app continue.\n * @param operation - name of the operation that failed\n * @param result - optional value to return as the observable result\n */\nprivate handleError<T>(operation = 'operation', result?: T) {\n return (error: any): Observable<T> => {\n\n // TODO: send the error to remote logging infrastructure\n console.error(error); // log to console instead\n\n // TODO: better job of transforming error for user consumption\n this.log(`${operation} failed: ${error.message}`);\n\n // Let the app keep running by returning an empty result.\n return of(result as T);\n };\n}\n\n\n

After reporting the error to the console, the handler constructs\na user friendly message and returns a safe value to the application so the application can keep working.

\n

Because each service method returns a different kind of Observable result,\nhandleError() takes a type parameter so it can return the safe value as the type that the application expects.

\n

Tap into the Observablelink

\n

The HeroService methods will tap into the flow of observable values\nand send a message, via the log() method, to the message area at the bottom of the page.

\n

They'll do that with the RxJS tap() operator,\nwhich looks at the observable values, does something with those values,\nand passes them along.\nThe tap() call back doesn't touch the values themselves.

\n

Here is the final version of getHeroes() with the tap() that logs the operation.

\n\n/** GET heroes from the server */\ngetHeroes(): Observable<Hero[]> {\n return this.http.get<Hero[]>(this.heroesUrl)\n .pipe(\n tap(_ => this.log('fetched heroes')),\n catchError(this.handleError<Hero[]>('getHeroes', []))\n );\n}\n\n\n

Get hero by idlink

\n

Most web APIs support a get by id request in the form :baseURL/:id.

\n

Here, the base URL is the heroesURL defined in the Heroes and HTTP section (api/heroes) and id is\nthe number of the hero that you want to retrieve. For example, api/heroes/11.

\n

Update the HeroService getHero() method with the following to make that request:

\n\n/** GET hero by id. Will 404 if id not found */\ngetHero(id: number): Observable<Hero> {\n const url = `${this.heroesUrl}/${id}`;\n return this.http.get<Hero>(url).pipe(\n tap(_ => this.log(`fetched hero id=${id}`)),\n catchError(this.handleError<Hero>(`getHero id=${id}`))\n );\n}\n\n\n

There are three significant differences from getHeroes():

\n\n

Update heroeslink

\n

Edit a hero's name in the hero detail view.\nAs you type, the hero name updates the heading at the top of the page.\nBut when you click the \"go back button\", the changes are lost.

\n

If you want changes to persist, you must write them back to\nthe server.

\n

At the end of the hero detail template, add a save button with a click event\nbinding that invokes a new component method named save().

\n\n<button (click)=\"save()\">save</button>\n\n\n

In the HeroDetail component class, add the following save() method, which persists hero name changes using the hero service\nupdateHero() method and then navigates back to the previous view.

\n\nsave(): void {\n this.heroService.updateHero(this.hero)\n .subscribe(() => this.goBack());\n}\n\n\n

Add HeroService.updateHero()link

\n

The overall structure of the updateHero() method is similar to that of\ngetHeroes(), but it uses http.put() to persist the changed hero\non the server. Add the following to the HeroService.

\n\n/** PUT: update the hero on the server */\nupdateHero(hero: Hero): Observable<any> {\n return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(\n tap(_ => this.log(`updated hero id=${hero.id}`)),\n catchError(this.handleError<any>('updateHero'))\n );\n}\n\n\n

The HttpClient.put() method takes three parameters:

\n\n

The URL is unchanged. The heroes web API knows which hero to update by looking at the hero's id.

\n

The heroes web API expects a special header in HTTP save requests.\nThat header is in the httpOptions constant defined in the HeroService. Add the following to the HeroService class.

\n\nhttpOptions = {\n headers: new HttpHeaders({ 'Content-Type': 'application/json' })\n};\n\n\n

Refresh the browser, change a hero name and save your change. The save()\nmethod in HeroDetailComponent navigates to the previous view.\nThe hero now appears in the list with the changed name.

\n

Add a new herolink

\n

To add a hero, this application only needs the hero's name. You can use an <input>\nelement paired with an add button.

\n

Insert the following into the HeroesComponent template, just after\nthe heading:

\n\n<div>\n <label id=\"new-hero\">Hero name: </label>\n <input for=\"new-hero\" #heroName />\n\n <!-- (click) passes input value to add() and then clears the input -->\n <button class=\"add-button\" (click)=\"add(heroName.value); heroName.value=''\">\n Add hero\n </button>\n</div>\n\n\n

In response to a click event, call the component's click handler, add(), and then\nclear the input field so that it's ready for another name. Add the following to the\nHeroesComponent class:

\n\nadd(name: string): void {\n name = name.trim();\n if (!name) { return; }\n this.heroService.addHero({ name } as Hero)\n .subscribe(hero => {\n this.heroes.push(hero);\n });\n}\n\n\n

When the given name is non-blank, the handler creates a Hero-like object\nfrom the name (it's only missing the id) and passes it to the services addHero() method.

\n

When addHero() saves successfully, the subscribe() callback\nreceives the new hero and pushes it into to the heroes list for display.

\n

Add the following addHero() method to the HeroService class.

\n\n/** POST: add a new hero to the server */\naddHero(hero: Hero): Observable<Hero> {\n return this.http.post<Hero>(this.heroesUrl, hero, this.httpOptions).pipe(\n tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),\n catchError(this.handleError<Hero>('addHero'))\n );\n}\n\n\n

addHero() differs from updateHero() in two ways:

\n\n

Refresh the browser and add some heroes.

\n

Delete a herolink

\n

Each hero in the heroes list should have a delete button.

\n

Add the following button element to the HeroesComponent template, after the hero\nname in the repeated <li> element.

\n\n<button class=\"delete\" title=\"delete hero\"\n (click)=\"delete(hero)\">x</button>\n\n\n

The HTML for the list of heroes should look like this:

\n\n<ul class=\"heroes\">\n <li *ngFor=\"let hero of heroes\">\n <a routerLink=\"/detail/{{hero.id}}\">\n <span class=\"badge\">{{hero.id}}</span> {{hero.name}}\n </a>\n <button class=\"delete\" title=\"delete hero\"\n (click)=\"delete(hero)\">x</button>\n </li>\n</ul>\n\n\n

To position the delete button at the far right of the hero entry,\nadd some CSS to the heroes.component.css. You'll find that CSS\nin the final review code below.

\n

Add the delete() handler to the component class.

\n\ndelete(hero: Hero): void {\n this.heroes = this.heroes.filter(h => h !== hero);\n this.heroService.deleteHero(hero.id).subscribe();\n}\n\n\n

Although the component delegates hero deletion to the HeroService,\nit remains responsible for updating its own list of heroes.\nThe component's delete() method immediately removes the hero-to-delete from that list,\nanticipating that the HeroService will succeed on the server.

\n

There's really nothing for the component to do with the Observable returned by\nheroService.delete() but it must subscribe anyway.

\n
\n

If you neglect to subscribe(), the service will not send the delete request to the server.\nAs a rule, an Observable does nothing until something subscribes.

\n

Confirm this for yourself by temporarily removing the subscribe(),\nclicking \"Dashboard\", then clicking \"Heroes\".\nYou'll see the full list of heroes again.

\n
\n

Next, add a deleteHero() method to HeroService like this.

\n\n/** DELETE: delete the hero from the server */\ndeleteHero(id: number): Observable<Hero> {\n const url = `${this.heroesUrl}/${id}`;\n\n return this.http.delete<Hero>(url, this.httpOptions).pipe(\n tap(_ => this.log(`deleted hero id=${id}`)),\n catchError(this.handleError<Hero>('deleteHero'))\n );\n}\n\n\n

Note the following key points:

\n\n

Refresh the browser and try the new delete functionality.

\n

Search by namelink

\n

In this last exercise, you learn to chain Observable operators together\nso you can minimize the number of similar HTTP requests\nand consume network bandwidth economically.

\n

You will add a heroes search feature to the Dashboard.\nAs the user types a name into a search box,\nyou'll make repeated HTTP requests for heroes filtered by that name.\nYour goal is to issue only as many requests as necessary.

\n

HeroService.searchHeroes()link

\n

Start by adding a searchHeroes() method to the HeroService.

\n\n/* GET heroes whose name contains search term */\nsearchHeroes(term: string): Observable<Hero[]> {\n if (!term.trim()) {\n // if not search term, return empty hero array.\n return of([]);\n }\n return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(\n tap(x => x.length ?\n this.log(`found heroes matching \"${term}\"`) :\n this.log(`no heroes matching \"${term}\"`)),\n catchError(this.handleError<Hero[]>('searchHeroes', []))\n );\n}\n\n\n

The method returns immediately with an empty array if there is no search term.\nThe rest of it closely resembles getHeroes(), the only significant difference being\nthe URL, which includes a query string with the search term.

\n

Add search to the Dashboardlink

\n

Open the DashboardComponent template and\nadd the hero search element, <app-hero-search>, to the bottom of the markup.

\n\n<h2>Top Heroes</h2>\n<div class=\"heroes-menu\">\n <a *ngFor=\"let hero of heroes\"\n routerLink=\"/detail/{{hero.id}}\">\n {{hero.name}}\n </a>\n</div>\n\n<app-hero-search></app-hero-search>\n\n\n\n

This template looks a lot like the *ngFor repeater in the HeroesComponent template.

\n

For this to work, the next step is to add a component with a selector that matches <app-hero-search>.

\n

Create HeroSearchComponentlink

\n

Create a HeroSearchComponent with the CLI.

\n\n ng generate component hero-search\n\n

The CLI generates the three HeroSearchComponent files and adds the component to the AppModule declarations.

\n

Replace the generated HeroSearchComponent template with an <input> and a list of matching search results, as follows.

\n\n<div id=\"search-component\">\n <label for=\"search-box\">Hero Search</label>\n <input #searchBox id=\"search-box\" (input)=\"search(searchBox.value)\" />\n\n <ul class=\"search-result\">\n <li *ngFor=\"let hero of heroes$ | async\" >\n <a routerLink=\"/detail/{{hero.id}}\">\n {{hero.name}}\n </a>\n </li>\n </ul>\n</div>\n\n\n\n

Add private CSS styles to hero-search.component.css\nas listed in the final code review below.

\n

As the user types in the search box, an input event binding calls the\ncomponent's search() method with the new search box value.

\n\n

AsyncPipelink

\n

The *ngFor repeats hero objects. Notice that the *ngFor iterates over a list called heroes$, not heroes. The $ is a convention that indicates heroes$ is an Observable, not an array.

\n\n<li *ngFor=\"let hero of heroes$ | async\" >\n\n\n

Since *ngFor can't do anything with an Observable, use the\npipe character (|) followed by async. This identifies Angular's AsyncPipe and subscribes to an Observable automatically so you won't have to\ndo so in the component class.

\n

Edit the HeroSearchComponent classlink

\n

Replace the generated HeroSearchComponent class and metadata as follows.

\n\nimport { Component, OnInit } from '@angular/core';\n\nimport { Observable, Subject } from 'rxjs';\n\nimport {\n debounceTime, distinctUntilChanged, switchMap\n } from 'rxjs/operators';\n\nimport { Hero } from '../hero';\nimport { HeroService } from '../hero.service';\n\n@Component({\n selector: 'app-hero-search',\n templateUrl: './hero-search.component.html',\n styleUrls: [ './hero-search.component.css' ]\n})\nexport class HeroSearchComponent implements OnInit {\n heroes$: Observable<Hero[]>;\n private searchTerms = new Subject<string>();\n\n constructor(private heroService: HeroService) {}\n\n // Push a search term into the observable stream.\n search(term: string): void {\n this.searchTerms.next(term);\n }\n\n ngOnInit(): void {\n this.heroes$ = this.searchTerms.pipe(\n // wait 300ms after each keystroke before considering the term\n debounceTime(300),\n\n // ignore new term if same as previous term\n distinctUntilChanged(),\n\n // switch to new search observable each time the term changes\n switchMap((term: string) => this.heroService.searchHeroes(term)),\n );\n }\n}\n\n\n\n

Notice the declaration of heroes$ as an Observable:

\n\nheroes$: Observable<Hero[]>;\n\n\n

You'll set it in ngOnInit().\nBefore you do, focus on the definition of searchTerms.

\n

The searchTerms RxJS subjectlink

\n

The searchTerms property is an RxJS Subject.

\n\nprivate searchTerms = new Subject<string>();\n\n// Push a search term into the observable stream.\nsearch(term: string): void {\n this.searchTerms.next(term);\n}\n\n\n

A Subject is both a source of observable values and an Observable itself.\nYou can subscribe to a Subject as you would any Observable.

\n

You can also push values into that Observable by calling its next(value) method\nas the search() method does.

\n

The event binding to the textbox's input event calls the search() method.

\n\n<input #searchBox id=\"search-box\" (input)=\"search(searchBox.value)\" />\n\n\n

Every time the user types in the textbox, the binding calls search() with the textbox value, a \"search term\".\nThe searchTerms becomes an Observable emitting a steady stream of search terms.

\n\n

Chaining RxJS operatorslink

\n

Passing a new search term directly to the searchHeroes() after every user keystroke would create an excessive amount of HTTP requests,\ntaxing server resources and burning through data plans.

\n

Instead, the ngOnInit() method pipes the searchTerms observable through a sequence of RxJS operators that reduce the number of calls to the searchHeroes(),\nultimately returning an observable of timely hero search results (each a Hero[]).

\n

Here's a closer look at the code.

\n\nthis.heroes$ = this.searchTerms.pipe(\n // wait 300ms after each keystroke before considering the term\n debounceTime(300),\n\n // ignore new term if same as previous term\n distinctUntilChanged(),\n\n // switch to new search observable each time the term changes\n switchMap((term: string) => this.heroService.searchHeroes(term)),\n);\n\n\n

Each operator works as follows:

\n\n
\n

With the switchMap operator,\nevery qualifying key event can trigger an HttpClient.get() method call.\nEven with a 300ms pause between requests, you could have multiple HTTP requests in flight\nand they may not return in the order sent.

\n

switchMap() preserves the original request order while returning only the observable from the most recent HTTP method call.\nResults from prior calls are canceled and discarded.

\n

Note that canceling a previous searchHeroes() Observable\ndoesn't actually abort a pending HTTP request.\nUnwanted results are discarded before they reach your application code.

\n
\n

Remember that the component class does not subscribe to the heroes$ observable.\nThat's the job of the AsyncPipe in the template.

\n

Try itlink

\n

Run the application again. In the Dashboard, enter some text in the search box.\nIf you enter characters that match any existing hero names, you'll see something like this.

\n
\n \"Hero\n
\n

Final code reviewlink

\n

Here are the code files discussed on this page (all in the src/app/ folder).

\n\n\n\n

HeroService, InMemoryDataService, AppModulelink

\n\n \nimport { Injectable } from '@angular/core';\nimport { HttpClient, HttpHeaders } from '@angular/common/http';\n\nimport { Observable, of } from 'rxjs';\nimport { catchError, map, tap } from 'rxjs/operators';\n\nimport { Hero } from './hero';\nimport { MessageService } from './message.service';\n\n\n@Injectable({ providedIn: 'root' })\nexport class HeroService {\n\n private heroesUrl = 'api/heroes'; // URL to web api\n\n httpOptions = {\n headers: new HttpHeaders({ 'Content-Type': 'application/json' })\n };\n\n constructor(\n private http: HttpClient,\n private messageService: MessageService) { }\n\n /** GET heroes from the server */\n getHeroes(): Observable<Hero[]> {\n return this.http.get<Hero[]>(this.heroesUrl)\n .pipe(\n tap(_ => this.log('fetched heroes')),\n catchError(this.handleError<Hero[]>('getHeroes', []))\n );\n }\n\n /** GET hero by id. Return `undefined` when id not found */\n getHeroNo404<Data>(id: number): Observable<Hero> {\n const url = `${this.heroesUrl}/?id=${id}`;\n return this.http.get<Hero[]>(url)\n .pipe(\n map(heroes => heroes[0]), // returns a {0|1} element array\n tap(h => {\n const outcome = h ? `fetched` : `did not find`;\n this.log(`${outcome} hero id=${id}`);\n }),\n catchError(this.handleError<Hero>(`getHero id=${id}`))\n );\n }\n\n /** GET hero by id. Will 404 if id not found */\n getHero(id: number): Observable<Hero> {\n const url = `${this.heroesUrl}/${id}`;\n return this.http.get<Hero>(url).pipe(\n tap(_ => this.log(`fetched hero id=${id}`)),\n catchError(this.handleError<Hero>(`getHero id=${id}`))\n );\n }\n\n /* GET heroes whose name contains search term */\n searchHeroes(term: string): Observable<Hero[]> {\n if (!term.trim()) {\n // if not search term, return empty hero array.\n return of([]);\n }\n return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(\n tap(x => x.length ?\n this.log(`found heroes matching \"${term}\"`) :\n this.log(`no heroes matching \"${term}\"`)),\n catchError(this.handleError<Hero[]>('searchHeroes', []))\n );\n }\n\n //////// Save methods //////////\n\n /** POST: add a new hero to the server */\n addHero(hero: Hero): Observable<Hero> {\n return this.http.post<Hero>(this.heroesUrl, hero, this.httpOptions).pipe(\n tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),\n catchError(this.handleError<Hero>('addHero'))\n );\n }\n\n /** DELETE: delete the hero from the server */\n deleteHero(id: number): Observable<Hero> {\n const url = `${this.heroesUrl}/${id}`;\n\n return this.http.delete<Hero>(url, this.httpOptions).pipe(\n tap(_ => this.log(`deleted hero id=${id}`)),\n catchError(this.handleError<Hero>('deleteHero'))\n );\n }\n\n /** PUT: update the hero on the server */\n updateHero(hero: Hero): Observable<any> {\n return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(\n tap(_ => this.log(`updated hero id=${hero.id}`)),\n catchError(this.handleError<any>('updateHero'))\n );\n }\n\n /**\n * Handle Http operation that failed.\n * Let the app continue.\n * @param operation - name of the operation that failed\n * @param result - optional value to return as the observable result\n */\n private handleError<T>(operation = 'operation', result?: T) {\n return (error: any): Observable<T> => {\n\n // TODO: send the error to remote logging infrastructure\n console.error(error); // log to console instead\n\n // TODO: better job of transforming error for user consumption\n this.log(`${operation} failed: ${error.message}`);\n\n // Let the app keep running by returning an empty result.\n return of(result as T);\n };\n }\n\n /** Log a HeroService message with the MessageService */\n private log(message: string) {\n this.messageService.add(`HeroService: ${message}`);\n }\n}\n\n\n\n \nimport { Injectable } from '@angular/core';\nimport { InMemoryDbService } from 'angular-in-memory-web-api';\nimport { Hero } from './hero';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class InMemoryDataService implements InMemoryDbService {\n createDb() {\n const heroes = [\n { id: 11, name: 'Dr Nice' },\n { id: 12, name: 'Narco' },\n { id: 13, name: 'Bombasto' },\n { id: 14, name: 'Celeritas' },\n { id: 15, name: 'Magneta' },\n { id: 16, name: 'RubberMan' },\n { id: 17, name: 'Dynama' },\n { id: 18, name: 'Dr IQ' },\n { id: 19, name: 'Magma' },\n { id: 20, name: 'Tornado' }\n ];\n return {heroes};\n }\n\n // Overrides the genId method to ensure that a hero always has an id.\n // If the heroes array is empty,\n // the method below returns the initial number (11).\n // if the heroes array is not empty, the method below returns the highest\n // hero id + 1.\n genId(heroes: Hero[]): number {\n return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11;\n }\n}\n\n\n\n \nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { FormsModule } from '@angular/forms';\nimport { HttpClientModule } from '@angular/common/http';\n\nimport { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';\nimport { InMemoryDataService } from './in-memory-data.service';\n\nimport { AppRoutingModule } from './app-routing.module';\n\nimport { AppComponent } from './app.component';\nimport { DashboardComponent } from './dashboard/dashboard.component';\nimport { HeroDetailComponent } from './hero-detail/hero-detail.component';\nimport { HeroesComponent } from './heroes/heroes.component';\nimport { HeroSearchComponent } from './hero-search/hero-search.component';\nimport { MessagesComponent } from './messages/messages.component';\n\n@NgModule({\n imports: [\n BrowserModule,\n FormsModule,\n AppRoutingModule,\n HttpClientModule,\n\n // The HttpClientInMemoryWebApiModule module intercepts HTTP requests\n // and returns simulated server responses.\n // Remove it when a real server is ready to receive requests.\n HttpClientInMemoryWebApiModule.forRoot(\n InMemoryDataService, { dataEncapsulation: false }\n )\n ],\n declarations: [\n AppComponent,\n DashboardComponent,\n HeroesComponent,\n HeroDetailComponent,\n MessagesComponent,\n HeroSearchComponent\n ],\n bootstrap: [ AppComponent ]\n})\nexport class AppModule { }\n\n\n\n\n\n

HeroesComponentlink

\n\n \n<h2>My Heroes</h2>\n\n<div>\n <label id=\"new-hero\">Hero name: </label>\n <input for=\"new-hero\" #heroName />\n\n <!-- (click) passes input value to add() and then clears the input -->\n <button class=\"add-button\" (click)=\"add(heroName.value); heroName.value=''\">\n Add hero\n </button>\n</div>\n\n<ul class=\"heroes\">\n <li *ngFor=\"let hero of heroes\">\n <a routerLink=\"/detail/{{hero.id}}\">\n <span class=\"badge\">{{hero.id}}</span> {{hero.name}}\n </a>\n <button class=\"delete\" title=\"delete hero\"\n (click)=\"delete(hero)\">x</button>\n </li>\n</ul>\n\n\n\n \nimport { Component, OnInit } from '@angular/core';\n\nimport { Hero } from '../hero';\nimport { HeroService } from '../hero.service';\n\n@Component({\n selector: 'app-heroes',\n templateUrl: './heroes.component.html',\n styleUrls: ['./heroes.component.css']\n})\nexport class HeroesComponent implements OnInit {\n heroes: Hero[];\n\n constructor(private heroService: HeroService) { }\n\n ngOnInit() {\n this.getHeroes();\n }\n\n getHeroes(): void {\n this.heroService.getHeroes()\n .subscribe(heroes => this.heroes = heroes);\n }\n\n add(name: string): void {\n name = name.trim();\n if (!name) { return; }\n this.heroService.addHero({ name } as Hero)\n .subscribe(hero => {\n this.heroes.push(hero);\n });\n }\n\n delete(hero: Hero): void {\n this.heroes = this.heroes.filter(h => h !== hero);\n this.heroService.deleteHero(hero.id).subscribe();\n }\n\n}\n\n\n\n \n/* HeroesComponent's private CSS styles */\n.heroes {\n margin: 0 0 2em 0;\n list-style-type: none;\n padding: 0;\n width: 15em;\n}\n\ninput {\n display: block;\n width: 100%;\n padding: .5rem;\n margin: 1rem 0;\n box-sizing: border-box;\n}\n\n.heroes li {\n position: relative;\n cursor: pointer;\n}\n\n.heroes li:hover {\n left: .1em;\n}\n\n.heroes a {\n color: #333;\n text-decoration: none;\n background-color: #EEE;\n margin: .5em;\n padding: .3em 0;\n height: 1.6em;\n border-radius: 4px;\n display: block;\n width: 100%;\n}\n\n.heroes a:hover {\n color: #2c3a41;\n background-color: #e6e6e6;\n}\n\n.heroes a:active {\n background-color: #525252;\n color: #fafafa;\n}\n\n.heroes .badge {\n display: inline-block;\n font-size: small;\n color: white;\n padding: 0.8em 0.7em 0 0.7em;\n background-color:#405061;\n line-height: 1em;\n position: relative;\n left: -1px;\n top: -4px;\n height: 1.8em;\n min-width: 16px;\n text-align: right;\n margin-right: .8em;\n border-radius: 4px 0 0 4px;\n}\n\n.add-button {\n padding: .5rem 1.5rem;\n font-size: 1rem;\n margin-bottom: 2rem;\n}\n\n.add-button:hover {\n color: white;\n background-color: #42545C;\n}\n\nbutton.delete {\n position: absolute;\n left: 210px;\n top: 5px;\n background-color: white;\n color: #525252;\n font-size: 1.1rem;\n padding: 1px 10px 3px 10px;\n}\n\nbutton.delete:hover {\n background-color: #525252;\n color: white;\n}\n\n\n\n\n\n

HeroDetailComponentlink

\n\n \n<div *ngIf=\"hero\">\n <h2>{{hero.name | uppercase}} Details</h2>\n <div><span>id: </span>{{hero.id}}</div>\n <div>\n <label for=\"hero-name\">Hero name: </label>\n <input id=\"hero-name\" [(ngModel)]=\"hero.name\" placeholder=\"Hero name\"/>\n </div>\n <button (click)=\"goBack()\">go back</button>\n <button (click)=\"save()\">save</button>\n</div>\n\n\n\n \nimport { Component, OnInit } from '@angular/core';\nimport { ActivatedRoute } from '@angular/router';\nimport { Location } from '@angular/common';\n\nimport { Hero } from '../hero';\nimport { HeroService } from '../hero.service';\n\n@Component({\n selector: 'app-hero-detail',\n templateUrl: './hero-detail.component.html',\n styleUrls: [ './hero-detail.component.css' ]\n})\nexport class HeroDetailComponent implements OnInit {\n hero: Hero;\n\n constructor(\n private route: ActivatedRoute,\n private heroService: HeroService,\n private location: Location\n ) {}\n\n ngOnInit(): void {\n this.getHero();\n }\n\n getHero(): void {\n const id = +this.route.snapshot.paramMap.get('id');\n this.heroService.getHero(id)\n .subscribe(hero => this.hero = hero);\n }\n\n goBack(): void {\n this.location.back();\n }\n\n save(): void {\n this.heroService.updateHero(this.hero)\n .subscribe(() => this.goBack());\n }\n}\n\n\n\n\n\n

DashboardComponentlink

\n\n \n<h2>Top Heroes</h2>\n<div class=\"heroes-menu\">\n <a *ngFor=\"let hero of heroes\"\n routerLink=\"/detail/{{hero.id}}\">\n {{hero.name}}\n </a>\n</div>\n\n<app-hero-search></app-hero-search>\n\n\n\n\n\n

HeroSearchComponentlink

\n\n \n<div id=\"search-component\">\n <label for=\"search-box\">Hero Search</label>\n <input #searchBox id=\"search-box\" (input)=\"search(searchBox.value)\" />\n\n <ul class=\"search-result\">\n <li *ngFor=\"let hero of heroes$ | async\" >\n <a routerLink=\"/detail/{{hero.id}}\">\n {{hero.name}}\n </a>\n </li>\n </ul>\n</div>\n\n\n\n \nimport { Component, OnInit } from '@angular/core';\n\nimport { Observable, Subject } from 'rxjs';\n\nimport {\n debounceTime, distinctUntilChanged, switchMap\n } from 'rxjs/operators';\n\nimport { Hero } from '../hero';\nimport { HeroService } from '../hero.service';\n\n@Component({\n selector: 'app-hero-search',\n templateUrl: './hero-search.component.html',\n styleUrls: [ './hero-search.component.css' ]\n})\nexport class HeroSearchComponent implements OnInit {\n heroes$: Observable<Hero[]>;\n private searchTerms = new Subject<string>();\n\n constructor(private heroService: HeroService) {}\n\n // Push a search term into the observable stream.\n search(term: string): void {\n this.searchTerms.next(term);\n }\n\n ngOnInit(): void {\n this.heroes$ = this.searchTerms.pipe(\n // wait 300ms after each keystroke before considering the term\n debounceTime(300),\n\n // ignore new term if same as previous term\n distinctUntilChanged(),\n\n // switch to new search observable each time the term changes\n switchMap((term: string) => this.heroService.searchHeroes(term)),\n );\n }\n}\n\n\n\n \n/* HeroSearch private styles */\n\nlabel {\n display: block;\n font-weight: bold;\n font-size: 1.2rem;\n margin-top: 1rem;\n margin-bottom: .5rem;\n\n}\ninput {\n padding: .5rem;\n width: 100%;\n max-width: 600px;\n box-sizing: border-box;\n display: block;\n}\n\ninput:focus {\n outline: #336699 auto 1px;\n}\n\nli {\n list-style-type: none;\n}\n.search-result li a {\n border-bottom: 1px solid gray;\n border-left: 1px solid gray;\n border-right: 1px solid gray;\n display: inline-block;\n width: 100%;\n max-width: 600px;\n padding: .5rem;\n box-sizing: border-box;\n text-decoration: none;\n color: black;\n}\n\n.search-result li a:hover {\n background-color: #435A60;\n color: white;\n}\n\nul.search-result {\n margin-top: 0;\n padding-left: 0;\n}\n\n\n\n\n

Summarylink

\n

You're at the end of your journey, and you've accomplished a lot.

\n\n

This concludes the \"Tour of Heroes\" tutorial.\nYou're ready to learn more about Angular development in the fundamentals section,\nstarting with the Architecture guide.

\n\n \n
\n\n\n" }