How to view multi-request loading with Ionic 3 and Angular 4

Asked

Viewed 331 times

1

How can I display a loading on the screen during the request time of 2 or more required with Ionic 3 and/or Angular 4?

I tried to call the loading in the two requests, but whenever the request finishes faster, it already hides the loading, without the second is finished.

ionViewDidLoad() {
  this.getSummary(parametros);
  this.getHeatmap(parametros);
}

getSummary() {
  this.loadingService.showLoader(this.constants.MSG_LOADING);

  this.remedyProvider.getSummaryByDate().subscribe(
    (response) => {
     /// do something
    }, (error) => {
      this.showSummary = false;
      console.log(`Backend returned error was: ${error}`);
      this.loadingService.hideLoader();
    }, () => {
      console.log('get heatmap complete');
      this.loadingService.hideLoader();
    }););
}

getHeatmap() {
  this.loadingService.showLoader(this.constants.MSG_LOADING);

  this.remedyProvider.getHeatmap().subscribe(
    (response) => {
        //do something      
    }, (error) => {
      console.log(`Backend returned error was: ${error}`);

    }, () => {
      console.log('get heatmap complete');
      this.loadingService.hideLoader();
    });
}

Code of Loadingprovider (loadginService):

export class LoadingProvider {

  loader: any = null;
  constructor(private _loadingController: LoadingController) {
  }

  private showLoadingHandler(message) {
      if (this.loader == null) {
          this.loader = this._loadingController.create({
              content: message
          });
          this.loader.present();
      } else {
          this.loader.data.content = message;
      }
  }

  private hideLoadingHandler() {
      if (this.loader != null) {
          this.loader.dismiss();
          this.loader = null;
      }
  }

  public showLoader(message) {
      this.showLoadingHandler(message);
  }

  public hideLoader() {
      this.hideLoadingHandler();
  }
}

1 answer

1


I think that’s what you need... In order for Loading to run while requests are being processed, you can use it as follows:

let lr = this.loadingCtrl.create({
  content: 'Carregando...',
});

lr.present().then(() => {
    //suas requisições aqui.
    //ao final da requisição, use:
    ()=> lr.dismiss()
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.