Tiffgray34
Tiffgray34

Reputation: 61

Inspect HTTP status in the service - Angular6

I'm working on Angular6 project and working fine now. I'm trying to add a spinner while http request.

Angular service used for all the http requests.

How can I inspect the http status while requesting so that I can show spinner.

There are bunch of http requests so I don't want add spinner manually for all the requests.

How can I achieve this with less scripts in Angular6? (all in one place if possible)

Upvotes: 1

Views: 43

Answers (1)

JNC
JNC

Reputation: 36

Firstly, to make the service calls you might be using Observables.

1) Add a spinner template to your html page with *ngIf for a boolean variable.

2) Before subscribing to the observable i.e. before making the http request make the variable as true (this will show the spinner) and on receiving the response make the variable as false (this will hide the spinner).

Eg Code:

this.loader = true; //This line shows the loader
this.YourService.getData().subscribe((response)=> {
    this.loader = false; //This line hides the loader.
})

Upvotes: 1

Related Questions