Help Angular by taking a 1 minute survey!Go to surveyHome

HTTP

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

  • The HeroService gets hero data with HTTP requests.
  • Users can add, edit, and delete heroes and save these changes over HTTP.
  • Users can search for heroes by name.

When you're done with this page, the app should look like this live example / download example.

Enable HTTP services

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

To make HttpClient available everywhere in the app:

import { HttpClientModule } from '@angular/common/http';
src/app/app.module.ts (Http Client import)
      
      import { HttpClientModule }    from '@angular/common/http';
    

Simulate a data server

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

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

This facility is a great convenience for the tutorial. You won't have to set up a server to learn about HttpClient.

It may also be convenient in the early stages of your own app development when the server's web api is ill-defined or not yet implemented.

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

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

Install the In-memory Web API package from npm

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

Import the HttpClientInMemoryWebApiModule and the InMemoryDataService class, which you will create in a moment.

import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemoryDataService } from './in-memory-data.service';
src/app/app.module.ts (In-memory Web API imports)
      
      import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService }  from './in-memory-data.service';
    

Add the HttpClientInMemoryWebApiModule to the @NgModule.imports array— after importing the HttpClientModule, —while configuring it with the InMemoryDataService.

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

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

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

The class src/app/in-memory-data.service.ts is generated by the following command:

ng generate service InMemoryData
      
      ng generate service InMemoryData
    

This class has the following content:

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

@Injectable({
  providedIn: 'root',
})
export class InMemoryDataService implements InMemoryDbService {
  createDb() {
    const heroes = [
      { id: 11, name: 'Mr. Nice' },
      { id: 12, name: 'Narco' },
      { id: 13, name: 'Bombasto' },
      { id: 14, name: 'Celeritas' },
      { id: 15, name: 'Magneta' },
      { id: 16, name: 'RubberMan' },
      { id: 17, name: 'Dynama' },
      { id: 18, name: 'Dr IQ' },
      { id: 19, name: 'Magma' },
      { id: 20, name: 'Tornado' }
    ];
    return {heroes};
  }

  // Overrides the genId method to ensure that a hero always has an id.
  // If the heroes array is empty,
  // the method below returns the initial number (11).
  // if the heroes array is not empty, the method below returns the highest
  // hero id + 1.
  genId(heroes: Hero[]): number {
    return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11;
  }
}
    

This file replaces mock-heroes.ts, which is now safe to delete.

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

Now back to the HttpClient story.

Heroes and HTTP

Import some HTTP symbols that you'll need:

import { HttpClient, HttpHeaders } from '@angular/common/http';
src/app/hero.service.ts (import HTTP symbols)
      
      import { HttpClient, HttpHeaders } from '@angular/common/http';
    

Inject HttpClient into the constructor in a private property called http.

constructor( private http: HttpClient, private messageService: MessageService) { }
      
      constructor(
  private http: HttpClient,
  private messageService: MessageService) { }
    

Keep injecting the MessageService. You'll call it so frequently that you'll wrap it in a private log() method.

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

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

private heroesUrl = 'api/heroes'; // URL to web api
      
      private heroesUrl = 'api/heroes';  // URL to web api
    

Get heroes with HttpClient

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

getHeroes(): Observable<Hero[]> { return of(HEROES); }
src/app/hero.service.ts (getHeroes with RxJs 'of()')
      
      getHeroes(): Observable<Hero[]> {
  return of(HEROES);
}
    

Convert that method to use HttpClient

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

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

You've swapped of for http.get and the app keeps working without any other changes because both functions return an Observable<Hero[]>.

Http methods return one value

All HttpClient methods return an RxJS Observable of something.

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

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

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

HttpClient.get returns response data

HttpClient.get returns the body of the response as an untyped JSON object by default. Applying the optional type specifier, <Hero[]> , gives you a typed result object.

The shape of the JSON data is determined by the server's data API. The Tour of Heroes data API returns the hero data as an array.

Other APIs may bury the data that you want within an object. You might have to dig that data out by processing the Observable result with the RxJS map operator.

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

Error handling

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

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

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

import { catchError, map, tap } from 'rxjs/operators';
      
      import { catchError, map, tap } from 'rxjs/operators';
    

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

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

The catchError() operator intercepts an Observable that failed. It passes the error an error handler that can do what it wants with the error.

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

handleError

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

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

/** * Handle Http operation that failed. * Let the app continue. * @param operation - name of the operation that failed * @param result - optional value to return as the observable result */ private handleError<T> (operation = 'operation', result?: T) { return (error: any): Observable<T> => { // TODO: send the error to remote logging infrastructure console.error(error); // log to console instead // TODO: better job of transforming error for user consumption this.log(`${operation} failed: ${error.message}`); // Let the app keep running by returning an empty result. return of(result as T); }; }
      
      
  1. /**
  2. * Handle Http operation that failed.
  3. * Let the app continue.
  4. * @param operation - name of the operation that failed
  5. * @param result - optional value to return as the observable result
  6. */
  7. private handleError<T> (operation = 'operation', result?: T) {
  8. return (error: any): Observable<T> => {
  9.  
  10. // TODO: send the error to remote logging infrastructure
  11. console.error(error); // log to console instead
  12.  
  13. // TODO: better job of transforming error for user consumption
  14. this.log(`${operation} failed: ${error.message}`);
  15.  
  16. // Let the app keep running by returning an empty result.
  17. return of(result as T);
  18. };
  19. }

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

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

Tap into the Observable

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

They'll do that with the RxJS tap operator, which looks at the observable values, does something with those values, and passes them along. The tap call back doesn't touch the values themselves.

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

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

Get hero by id

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

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

Add a HeroService.getHero() method to make that request:

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

There are three significant differences from getHeroes().

  • it constructs a request URL with the desired hero's id.
  • the server should respond with a single hero rather than an array of heroes.
  • therefore, getHero returns an Observable<Hero> ("an observable of Hero objects") rather than an observable of hero arrays .

Update heroes

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

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

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

<button (click)="save()">save</button>
src/app/hero-detail/hero-detail.component.html (save)
      
      <button (click)="save()">save</button>
    

Add the following save() method, which persists hero name changes using the hero service updateHero() method and then navigates back to the previous view.

save(): void { this.heroService.updateHero(this.hero) .subscribe(() => this.goBack()); }
src/app/hero-detail/hero-detail.component.ts (save)
      
      save(): void {
   this.heroService.updateHero(this.hero)
     .subscribe(() => this.goBack());
 }
    

Add HeroService.updateHero()

The overall structure of the updateHero() method is similar to that of getHeroes(), but it uses http.put() to persist the changed hero on the server.

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

The HttpClient.put() method takes three parameters

  • the URL
  • the data to update (the modified hero in this case)
  • options

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

The heroes web API expects a special header in HTTP save requests. That header is in the httpOptions constant defined in the HeroService.

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
src/app/hero.service.ts
      
      const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
    

Refresh the browser, change a hero name and save your change. Navigating to the previous view is implemented in the save() method defined in HeroDetailComponent. The hero now appears in the list with the changed name.

Add a new hero

To add a hero, this app only needs the hero's name. You can use an input element paired with an add button.

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

<div> <label>Hero name: <input #heroName /> </label> <!-- (click) passes input value to add() and then clears the input --> <button (click)="add(heroName.value); heroName.value=''"> add </button> </div>
src/app/heroes/heroes.component.html (add)
      
      <div>
  <label>Hero name:
    <input #heroName />
  </label>
  <!-- (click) passes input value to add() and then clears the input -->
  <button (click)="add(heroName.value); heroName.value=''">
    add
  </button>
</div>
    

In response to a click event, call the component's click handler and then clear the input field so that it's ready for another name.

add(name: string): void { name = name.trim(); if (!name) { return; } this.heroService.addHero({ name } as Hero) .subscribe(hero => { this.heroes.push(hero); }); }
src/app/heroes/heroes.component.ts (add)
      
      add(name: string): void {
  name = name.trim();
  if (!name) { return; }
  this.heroService.addHero({ name } as Hero)
    .subscribe(hero => {
      this.heroes.push(hero);
    });
}
    

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

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

You'll write HeroService.addHero in the next section.

Add HeroService.addHero()

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

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

HeroService.addHero() differs from updateHero in two ways.

  • it calls HttpClient.post() instead of put().
  • it expects the server to generate an id for the new hero, which it returns in the Observable<Hero> to the caller.

Refresh the browser and add some heroes.

Delete a hero

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

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

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

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

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

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

Add the delete() handler to the component.

delete(hero: Hero): void { this.heroes = this.heroes.filter(h => h !== hero); this.heroService.deleteHero(hero).subscribe(); }
src/app/heroes/heroes.component.ts (delete)
      
      delete(hero: Hero): void {
  this.heroes = this.heroes.filter(h => h !== hero);
  this.heroService.deleteHero(hero).subscribe();
}
    

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

There's really nothing for the component to do with the Observable returned by heroService.delete(). It must subscribe anyway.

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

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

Add HeroService.deleteHero()

Add a deleteHero() method to HeroService like this.

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

  return this.http.delete<Hero>(url, httpOptions).pipe(
    tap(_ => this.log(`deleted hero id=${id}`)),
    catchError(this.handleError<Hero>('deleteHero'))
  );
}
    

Note that

  • it calls HttpClient.delete.
  • the URL is the heroes resource URL plus the id of the hero to delete
  • you don't send data as you did with put and post.
  • you still send the httpOptions.

Refresh the browser and try the new delete functionality.

Search by name

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

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

HeroService.searchHeroes

Start by adding a searchHeroes method to the HeroService.

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

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

Add search to the Dashboard

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

<h3>Top Heroes</h3> <div class="grid grid-pad"> <a *ngFor="let hero of heroes" class="col-1-4" routerLink="/detail/{{hero.id}}"> <div class="module hero"> <h4>{{hero.name}}</h4> </div> </a> </div> <app-hero-search></app-hero-search>
src/app/dashboard/dashboard.component.html
      
      <h3>Top Heroes</h3>
<div class="grid grid-pad">
  <a *ngFor="let hero of heroes" class="col-1-4"
      routerLink="/detail/{{hero.id}}">
    <div class="module hero">
      <h4>{{hero.name}}</h4>
    </div>
  </a>
</div>

<app-hero-search></app-hero-search>
    

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

Unfortunately, adding this element breaks the app. Angular can't find a component with a selector that matches <app-hero-search>.

The HeroSearchComponent doesn't exist yet. Fix that.

Create HeroSearchComponent

Create a HeroSearchComponent with the CLI.

ng generate component hero-search
      
      ng generate component hero-search
    

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

Replace the generated HeroSearchComponent template with a text box and a list of matching search results like this.

<div id="search-component"> <h4>Hero Search</h4> <input #searchBox id="search-box" (input)="search(searchBox.value)" /> <ul class="search-result"> <li *ngFor="let hero of heroes$ | async" > <a routerLink="/detail/{{hero.id}}"> {{hero.name}} </a> </li> </ul> </div>
src/app/hero-search/hero-search.component.html
      
      
  1. <div id="search-component">
  2. <h4>Hero Search</h4>
  3.  
  4. <input #searchBox id="search-box" (input)="search(searchBox.value)" />
  5.  
  6. <ul class="search-result">
  7. <li *ngFor="let hero of heroes$ | async" >
  8. <a routerLink="/detail/{{hero.id}}">
  9. {{hero.name}}
  10. </a>
  11. </li>
  12. </ul>
  13. </div>

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

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

AsyncPipe

As expected, the *ngFor repeats hero objects.

Look closely and you'll see that the *ngFor iterates over a list called heroes$, not heroes.

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

The $ is a convention that indicates heroes$ is an Observable, not an array.

The *ngFor can't do anything with an Observable. But there's also a pipe character (|) followed by async, which identifies Angular's AsyncPipe.

The AsyncPipe subscribes to an Observable automatically so you won't have to do so in the component class.

Fix the HeroSearchComponent class

Replace the generated HeroSearchComponent class and metadata as follows.

import { Component, OnInit } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-hero-search', templateUrl: './hero-search.component.html', styleUrls: [ './hero-search.component.css' ] }) export class HeroSearchComponent implements OnInit { heroes$: Observable<Hero[]>; private searchTerms = new Subject<string>(); constructor(private heroService: HeroService) {} // Push a search term into the observable stream. search(term: string): void { this.searchTerms.next(term); } ngOnInit(): void { this.heroes$ = this.searchTerms.pipe( // wait 300ms after each keystroke before considering the term debounceTime(300), // ignore new term if same as previous term distinctUntilChanged(), // switch to new search observable each time the term changes switchMap((term: string) => this.heroService.searchHeroes(term)), ); } }
src/app/hero-search/hero-search.component.ts
      
      
  1. import { Component, OnInit } from '@angular/core';
  2.  
  3. import { Observable, Subject } from 'rxjs';
  4.  
  5. import {
  6. debounceTime, distinctUntilChanged, switchMap
  7. } from 'rxjs/operators';
  8.  
  9. import { Hero } from '../hero';
  10. import { HeroService } from '../hero.service';
  11.  
  12. @Component({
  13. selector: 'app-hero-search',
  14. templateUrl: './hero-search.component.html',
  15. styleUrls: [ './hero-search.component.css' ]
  16. })
  17. export class HeroSearchComponent implements OnInit {
  18. heroes$: Observable<Hero[]>;
  19. private searchTerms = new Subject<string>();
  20.  
  21. constructor(private heroService: HeroService) {}
  22.  
  23. // Push a search term into the observable stream.
  24. search(term: string): void {
  25. this.searchTerms.next(term);
  26. }
  27.  
  28. ngOnInit(): void {
  29. this.heroes$ = this.searchTerms.pipe(
  30. // wait 300ms after each keystroke before considering the term
  31. debounceTime(300),
  32.  
  33. // ignore new term if same as previous term
  34. distinctUntilChanged(),
  35.  
  36. // switch to new search observable each time the term changes
  37. switchMap((term: string) => this.heroService.searchHeroes(term)),
  38. );
  39. }
  40. }

Notice the declaration of heroes$ as an Observable

heroes$: Observable<Hero[]>;
      
      heroes$: Observable<Hero[]>;
    

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

The searchTerms RxJS subject

The searchTerms property is declared as an RxJS Subject.

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

// Push a search term into the observable stream.
search(term: string): void {
  this.searchTerms.next(term);
}
    

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

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

The search() method is called via an event binding to the textbox's input event.

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

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

Chaining RxJS operators

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

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

Here's the code.

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

  // ignore new term if same as previous term
  distinctUntilChanged(),

  // switch to new search observable each time the term changes
  switchMap((term: string) => this.heroService.searchHeroes(term)),
);
    
  • debounceTime(300) waits until the flow of new string events pauses for 300 milliseconds before passing along the latest string. You'll never make requests more frequently than 300ms.
  • distinctUntilChanged() ensures that a request is sent only if the filter text changed.
  • switchMap() calls the search service for each search term that makes it through debounce and distinctUntilChanged. It cancels and discards previous search observables, returning only the latest search service observable.

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

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

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

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

Try it

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

Hero Search Component

Final code review

Your app should look like this live example / download example.

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

HeroService, InMemoryDataService, AppModule

import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable, of } from 'rxjs'; import { catchError, map, tap } from 'rxjs/operators'; import { Hero } from './hero'; import { MessageService } from './message.service'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; @Injectable({ providedIn: 'root' }) export class HeroService { private heroesUrl = 'api/heroes'; // URL to web api constructor( private http: HttpClient, private messageService: MessageService) { } /** GET heroes from the server */ getHeroes (): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) .pipe( tap(_ => this.log('fetched heroes')), catchError(this.handleError<Hero[]>('getHeroes', [])) ); } /** GET hero by id. Return `undefined` when id not found */ getHeroNo404<Data>(id: number): Observable<Hero> { const url = `${this.heroesUrl}/?id=${id}`; return this.http.get<Hero[]>(url) .pipe( map(heroes => heroes[0]), // returns a {0|1} element array tap(h => { const outcome = h ? `fetched` : `did not find`; this.log(`${outcome} hero id=${id}`); }), catchError(this.handleError<Hero>(`getHero id=${id}`)) ); } /** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable<Hero> { const url = `${this.heroesUrl}/${id}`; return this.http.get<Hero>(url).pipe( tap(_ => this.log(`fetched hero id=${id}`)), catchError(this.handleError<Hero>(`getHero id=${id}`)) ); } /* GET heroes whose name contains search term */ searchHeroes(term: string): Observable<Hero[]> { if (!term.trim()) { // if not search term, return empty hero array. return of([]); } return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe( tap(_ => this.log(`found heroes matching "${term}"`)), catchError(this.handleError<Hero[]>('searchHeroes', [])) ); } //////// Save methods ////////// /** POST: add a new hero to the server */ addHero (hero: Hero): Observable<Hero> { return this.http.post<Hero>(this.heroesUrl, hero, httpOptions).pipe( tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)), catchError(this.handleError<Hero>('addHero')) ); } /** DELETE: delete the hero from the server */ deleteHero (hero: Hero | number): Observable<Hero> { const id = typeof hero === 'number' ? hero : hero.id; const url = `${this.heroesUrl}/${id}`; return this.http.delete<Hero>(url, httpOptions).pipe( tap(_ => this.log(`deleted hero id=${id}`)), catchError(this.handleError<Hero>('deleteHero')) ); } /** PUT: update the hero on the server */ updateHero (hero: Hero): Observable<any> { return this.http.put(this.heroesUrl, hero, httpOptions).pipe( tap(_ => this.log(`updated hero id=${hero.id}`)), catchError(this.handleError<any>('updateHero')) ); } /** * Handle Http operation that failed. * Let the app continue. * @param operation - name of the operation that failed * @param result - optional value to return as the observable result */ private handleError<T> (operation = 'operation', result?: T) { return (error: any): Observable<T> => { // TODO: send the error to remote logging infrastructure console.error(error); // log to console instead // TODO: better job of transforming error for user consumption this.log(`${operation} failed: ${error.message}`); // Let the app keep running by returning an empty result. return of(result as T); }; } /** Log a HeroService message with the MessageService */ private log(message: string) { this.messageService.add(`HeroService: ${message}`); } } import { InMemoryDbService } from 'angular-in-memory-web-api'; import { Hero } from './hero'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class InMemoryDataService implements InMemoryDbService { createDb() { const heroes = [ { id: 11, name: 'Mr. Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombasto' }, { id: 14, name: 'Celeritas' }, { id: 15, name: 'Magneta' }, { id: 16, name: 'RubberMan' }, { id: 17, name: 'Dynama' }, { id: 18, name: 'Dr IQ' }, { id: 19, name: 'Magma' }, { id: 20, name: 'Tornado' } ]; return {heroes}; } // Overrides the genId method to ensure that a hero always has an id. // If the heroes array is empty, // the method below returns the initial number (11). // if the heroes array is not empty, the method below returns the highest // hero id + 1. genId(heroes: Hero[]): number { return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11; } } import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; import { InMemoryDataService } from './in-memory-data.service'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { DashboardComponent } from './dashboard/dashboard.component'; import { HeroDetailComponent } from './hero-detail/hero-detail.component'; import { HeroesComponent } from './heroes/heroes.component'; import { HeroSearchComponent } from './hero-search/hero-search.component'; import { MessagesComponent } from './messages/messages.component'; @NgModule({ imports: [ BrowserModule, FormsModule, AppRoutingModule, HttpClientModule, // The HttpClientInMemoryWebApiModule module intercepts HTTP requests // and returns simulated server responses. // Remove it when a real server is ready to receive requests. HttpClientInMemoryWebApiModule.forRoot( InMemoryDataService, { dataEncapsulation: false } ) ], declarations: [ AppComponent, DashboardComponent, HeroesComponent, HeroDetailComponent, MessagesComponent, HeroSearchComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
      
      
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3.  
  4. import { Observable, of } from 'rxjs';
  5. import { catchError, map, tap } from 'rxjs/operators';
  6.  
  7. import { Hero } from './hero';
  8. import { MessageService } from './message.service';
  9.  
  10. const httpOptions = {
  11. headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  12. };
  13.  
  14. @Injectable({ providedIn: 'root' })
  15. export class HeroService {
  16.  
  17. private heroesUrl = 'api/heroes'; // URL to web api
  18.  
  19. constructor(
  20. private http: HttpClient,
  21. private messageService: MessageService) { }
  22.  
  23. /** GET heroes from the server */
  24. getHeroes (): Observable<Hero[]> {
  25. return this.http.get<Hero[]>(this.heroesUrl)
  26. .pipe(
  27. tap(_ => this.log('fetched heroes')),
  28. catchError(this.handleError<Hero[]>('getHeroes', []))
  29. );
  30. }
  31.  
  32. /** GET hero by id. Return `undefined` when id not found */
  33. getHeroNo404<Data>(id: number): Observable<Hero> {
  34. const url = `${this.heroesUrl}/?id=${id}`;
  35. return this.http.get<Hero[]>(url)
  36. .pipe(
  37. map(heroes => heroes[0]), // returns a {0|1} element array
  38. tap(h => {
  39. const outcome = h ? `fetched` : `did not find`;
  40. this.log(`${outcome} hero id=${id}`);
  41. }),
  42. catchError(this.handleError<Hero>(`getHero id=${id}`))
  43. );
  44. }
  45.  
  46. /** GET hero by id. Will 404 if id not found */
  47. getHero(id: number): Observable<Hero> {
  48. const url = `${this.heroesUrl}/${id}`;
  49. return this.http.get<Hero>(url).pipe(
  50. tap(_ => this.log(`fetched hero id=${id}`)),
  51. catchError(this.handleError<Hero>(`getHero id=${id}`))
  52. );
  53. }
  54.  
  55. /* GET heroes whose name contains search term */
  56. searchHeroes(term: string): Observable<Hero[]> {
  57. if (!term.trim()) {
  58. // if not search term, return empty hero array.
  59. return of([]);
  60. }
  61. return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`).pipe(
  62. tap(_ => this.log(`found heroes matching "${term}"`)),
  63. catchError(this.handleError<Hero[]>('searchHeroes', []))
  64. );
  65. }
  66.  
  67. //////// Save methods //////////
  68.  
  69. /** POST: add a new hero to the server */
  70. addHero (hero: Hero): Observable<Hero> {
  71. return this.http.post<Hero>(this.heroesUrl, hero, httpOptions).pipe(
  72. tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
  73. catchError(this.handleError<Hero>('addHero'))
  74. );
  75. }
  76.  
  77. /** DELETE: delete the hero from the server */
  78. deleteHero (hero: Hero | number): Observable<Hero> {
  79. const id = typeof hero === 'number' ? hero : hero.id;
  80. const url = `${this.heroesUrl}/${id}`;
  81.  
  82. return this.http.delete<Hero>(url, httpOptions).pipe(
  83. tap(_ => this.log(`deleted hero id=${id}`)),
  84. catchError(this.handleError<Hero>('deleteHero'))
  85. );
  86. }
  87.  
  88. /** PUT: update the hero on the server */
  89. updateHero (hero: Hero): Observable<any> {
  90. return this.http.put(this.heroesUrl, hero, httpOptions).pipe(
  91. tap(_ => this.log(`updated hero id=${hero.id}`)),
  92. catchError(this.handleError<any>('updateHero'))
  93. );
  94. }
  95.  
  96. /**
  97. * Handle Http operation that failed.
  98. * Let the app continue.
  99. * @param operation - name of the operation that failed
  100. * @param result - optional value to return as the observable result
  101. */
  102. private handleError<T> (operation = 'operation', result?: T) {
  103. return (error: any): Observable<T> => {
  104.  
  105. // TODO: send the error to remote logging infrastructure
  106. console.error(error); // log to console instead
  107.  
  108. // TODO: better job of transforming error for user consumption
  109. this.log(`${operation} failed: ${error.message}`);
  110.  
  111. // Let the app keep running by returning an empty result.
  112. return of(result as T);
  113. };
  114. }
  115.  
  116. /** Log a HeroService message with the MessageService */
  117. private log(message: string) {
  118. this.messageService.add(`HeroService: ${message}`);
  119. }
  120. }

HeroesComponent

<h2>My Heroes</h2> <div> <label>Hero name: <input #heroName /> </label> <!-- (click) passes input value to add() and then clears the input --> <button (click)="add(heroName.value); heroName.value=''"> add </button> </div> <ul class="heroes"> <li *ngFor="let hero of heroes"> <a routerLink="/detail/{{hero.id}}"> <span class="badge">{{hero.id}}</span> {{hero.name}} </a> <button class="delete" title="delete hero" (click)="delete(hero)">x</button> </li> </ul> import { Component, OnInit } from '@angular/core'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-heroes', templateUrl: './heroes.component.html', styleUrls: ['./heroes.component.css'] }) export class HeroesComponent implements OnInit { heroes: Hero[]; constructor(private heroService: HeroService) { } ngOnInit() { this.getHeroes(); } getHeroes(): void { this.heroService.getHeroes() .subscribe(heroes => this.heroes = heroes); } add(name: string): void { name = name.trim(); if (!name) { return; } this.heroService.addHero({ name } as Hero) .subscribe(hero => { this.heroes.push(hero); }); } delete(hero: Hero): void { this.heroes = this.heroes.filter(h => h !== hero); this.heroService.deleteHero(hero).subscribe(); } } /* HeroesComponent's private CSS styles */ .heroes { margin: 0 0 2em 0; list-style-type: none; padding: 0; width: 15em; } .heroes li { position: relative; cursor: pointer; background-color: #EEE; margin: .5em; padding: .3em 0; height: 1.6em; border-radius: 4px; } .heroes li:hover { color: #607D8B; background-color: #DDD; left: .1em; } .heroes a { color: #888; text-decoration: none; position: relative; display: block; width: 250px; } .heroes a:hover { color:#607D8B; } .heroes .badge { display: inline-block; font-size: small; color: white; padding: 0.8em 0.7em 0 0.7em; background-color: #607D8B; line-height: 1em; position: relative; left: -1px; top: -4px; height: 1.8em; min-width: 16px; text-align: right; margin-right: .8em; border-radius: 4px 0 0 4px; } button { background-color: #eee; border: none; padding: 5px 10px; border-radius: 4px; cursor: pointer; cursor: hand; font-family: Arial; } button:hover { background-color: #cfd8dc; } button.delete { position: relative; left: 194px; top: -32px; background-color: gray !important; color: white; }
      
      
  1. <h2>My Heroes</h2>
  2.  
  3. <div>
  4. <label>Hero name:
  5. <input #heroName />
  6. </label>
  7. <!-- (click) passes input value to add() and then clears the input -->
  8. <button (click)="add(heroName.value); heroName.value=''">
  9. add
  10. </button>
  11. </div>
  12.  
  13. <ul class="heroes">
  14. <li *ngFor="let hero of heroes">
  15. <a routerLink="/detail/{{hero.id}}">
  16. <span class="badge">{{hero.id}}</span> {{hero.name}}
  17. </a>
  18. <button class="delete" title="delete hero"
  19. (click)="delete(hero)">x</button>
  20. </li>
  21. </ul>

HeroDetailComponent

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

DashboardComponent

<h3>Top Heroes</h3> <div class="grid grid-pad"> <a *ngFor="let hero of heroes" class="col-1-4" routerLink="/detail/{{hero.id}}"> <div class="module hero"> <h4>{{hero.name}}</h4> </div> </a> </div> <app-hero-search></app-hero-search>
      
      <h3>Top Heroes</h3>
<div class="grid grid-pad">
  <a *ngFor="let hero of heroes" class="col-1-4"
      routerLink="/detail/{{hero.id}}">
    <div class="module hero">
      <h4>{{hero.name}}</h4>
    </div>
  </a>
</div>

<app-hero-search></app-hero-search>
    

HeroSearchComponent

<div id="search-component"> <h4>Hero Search</h4> <input #searchBox id="search-box" (input)="search(searchBox.value)" /> <ul class="search-result"> <li *ngFor="let hero of heroes$ | async" > <a routerLink="/detail/{{hero.id}}"> {{hero.name}} </a> </li> </ul> </div> import { Component, OnInit } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; @Component({ selector: 'app-hero-search', templateUrl: './hero-search.component.html', styleUrls: [ './hero-search.component.css' ] }) export class HeroSearchComponent implements OnInit { heroes$: Observable<Hero[]>; private searchTerms = new Subject<string>(); constructor(private heroService: HeroService) {} // Push a search term into the observable stream. search(term: string): void { this.searchTerms.next(term); } ngOnInit(): void { this.heroes$ = this.searchTerms.pipe( // wait 300ms after each keystroke before considering the term debounceTime(300), // ignore new term if same as previous term distinctUntilChanged(), // switch to new search observable each time the term changes switchMap((term: string) => this.heroService.searchHeroes(term)), ); } } /* HeroSearch private styles */ .search-result li { border-bottom: 1px solid gray; border-left: 1px solid gray; border-right: 1px solid gray; width: 195px; height: 16px; padding: 5px; background-color: white; cursor: pointer; list-style-type: none; } .search-result li:hover { background-color: #607D8B; } .search-result li a { color: #888; display: block; text-decoration: none; } .search-result li a:hover { color: white; } .search-result li a:active { color: white; } #search-box { width: 200px; height: 20px; } ul.search-result { margin-top: 0; padding-left: 0; }
      
      
  1. <div id="search-component">
  2. <h4>Hero Search</h4>
  3.  
  4. <input #searchBox id="search-box" (input)="search(searchBox.value)" />
  5.  
  6. <ul class="search-result">
  7. <li *ngFor="let hero of heroes$ | async" >
  8. <a routerLink="/detail/{{hero.id}}">
  9. {{hero.name}}
  10. </a>
  11. </li>
  12. </ul>
  13. </div>

Summary

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

  • You added the necessary dependencies to use HTTP in the app.
  • You refactored HeroService to load heroes from a web API.
  • You extended HeroService to support post(), put(), and delete() methods.
  • You updated the components to allow adding, editing, and deleting of heroes.
  • You configured an in-memory web API.
  • You learned how to use observables.

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