Angular HttpClient - displays a spinner / progress indicator while waiting for a service response - progress events

I am switching my utility calls to use the new HttpClient. I struggle with 3 things

  • Find out how to show the progress bar / progress bar / etc while waiting for a response from the post, receive, deliver.
  • Fake slow response
  • Can new progress events be used to launch these kinds of functions?

application.component.ts

 this.applicationFormService.putForm(formModel)    
  .subscribe(
    // Successful responses call the first callback.
    (res) => this.goToRoute(res),
    // Errors will call this callback instead:
    (err: HttpErrorResponse) => {
      if (err.error instanceof Error) {
        console.log("Client-side error occured.");
      } else {
        console.log("Server-side error occured.");
      }
    },
    //Tried adding progress event logic here but I get error Argument of type '(event: any) => void' is not assignable to parameter of type '() => void'. Is this totally the wrong implementation and can it even be used for showing progress?
    event => {
      // Via this API, you get access to the raw event stream.
      // Look for upload progress events.
      if (event.type === HttpEventType.UploadProgress) {
        // This is an upload progress event.
      } else if (event instanceof HttpResponse) {

      }
    }
  )

application.service.ts

constructor (private httpNew: HttpClient){}
putForm(applicationForm:any){
 this.applicationId = this.id.id
 const req = new HttpRequest('PUT', this.applicationSubmitUrl+this.applicationId, applicationForm, {
  reportProgress: true,
 });
return this.httpNew.request(req)
}
+4
source share
2 answers

@aravind (https://alligator.io/angular/httpclient-intro/), . Angular Http, / , .

:

showSpinner = false;
this.shortFormService.postShortForm(formModel)
  .subscribe(      

    (event: HttpEvent<any>) => {
      console.log(event)
      switch (event.type) {
        case HttpEventType.Sent:
          this.showSpinner = true;
          console.log('Request sent!');
          break;
        case HttpEventType.ResponseHeader:
          console.log('Response header received!');
          break;
        case HttpEventType.UploadProgress:
          const percentDone = Math.round(100 * event.loaded / event.total);
          console.log(`File is ${percentDone}% uploaded.`);
        case HttpEventType.DownloadProgress:
          const kbLoaded = Math.round(event.loaded / 1024);
          console.log(`Download in progress! ${ kbLoaded }Kb loaded`);
          break;
        case HttpEventType.Response:
          console.log('😺 Done!', event.body);
          this.showSpinner = false;            

      }
    },
    (err: HttpErrorResponse) => {
      if (err.error instanceof Error) {
        console.log("Client-side error occured.");
      } else {
        this.router.navigate(['/error', err.error.error]);
        console.log("Server-side error occured.");
      }
    }
  )

}

:

postShortForm(shortForm: any) {
 const req = new HttpRequest('POST', this.preCheckUrl, shortForm, {
  reportProgress: true,
});
return this.httpNew.request(req)
  .retry(3)
}
+2

spinner,

import { Component, Input } from '@angular/core';

@Component({
    selector: 'spinner',
    template:
    ` <div *ngIf="show">
          <span><i class="fa fa-spinner fa-spin" [ngStyle]="{'font-size': size+'px'}" aria-hidden="true"></i></span>
      </div>
    `
})
export class SpinnerComponent {
    @Input() size: number = 25;
    @Input() show: boolean;


}

<spinner [show]="showSpinner" [size]="150"> </spinner>

typescript

this.applicationFormService.putForm(formModel)    
  .subscribe(data=>{
        ....
       // hide the spinner
       this.showSpinner = false;

    }(err: HttpErrorResponse) => {
       this.showSpinner = false;          
    })

, , onInit

ngOnInit(){
   this.showSpinner = true;
   ...service call logics...
}

LIVE DEMO

+4

Source: https://habr.com/ru/post/1683036/


All Articles