AngularJS Interview Questions Answers

How to intercept 404 errors in Angular 2?

The HTTP Interceptor service is used to handle the errors specific in Angular 2. The HTTP Interceptor service can be created and registered globally at the root module using the Angular Providers. Once it is defined at the root module; the HTTP Interceptor will intercept all the HTTP requests and the responses. So it makes an ideal place to catch all the common errors and handle them.

How to chain Http calls in Angular2?

Multiple HTTP calls in the Angular 2 can be chained using the mergeMap operator.

//example
import { mergeMap } from 'rxjs/operators';
this.http.get('./customer.json').pipe(
   mergeMap(customer => this.http.get(customer.contractUrl))
).subscribe(res => this.contract = res);

This merge operator is used by the Angular version above 4.3.