Ionic 3 show loading symbol on http request

馋奶兔 提交于 2019-12-25 01:45:07

问题


I am learning Ionic,so once the Signup button is clicked, How to show loading symbol and hide it after getting the reponse in ionic 3 ?

sign.html

<button ion-button color="danger" block outline (click)="signup()">Signup</button>

signup.ts

signup() {
    this.authServiceProvider.postData(this.userData, "signup").then((result) => {
    this.responseData = result;
    console.log(this.responseData);
    if( (JSON.stringify(this.responseData._body)) != "" ) {
        this.navCtrl.setRoot(HomePage);
    } else {
        console.log("User already exists");
    }
    }, (err) => {
        //connection failed error message
        console.log("something went wrong");
    });
}

回答1:


First of all, you need to import loading controller.

import { LoadingController } from 'ionic-angular';

in the constructor, you need to create an object of it. as

constructor(public loadingCtrl: LoadingController){}

Now before calling the service in signup method, you need to activate loading message and after the result, dismiss it.

signup() {
 let loading = this.loadingCtrl.create({
      content: 'Please wait...'
    });
    loading.present();
    this.authServiceProvider.postData(this.userData, "signup").then((result) => {
    this.responseData = result;
    console.log(this.responseData);
    if( (JSON.stringify(this.responseData._body)) != "" ) {
     loading.dismiss();        
     this.navCtrl.setRoot(HomePage);
    } else {
        loading.dismiss();
        console.log("User already exists");
    }
    }, (err) => {
        //connection failed error message
        console.log("something went wrong");
    });
}


来源:https://stackoverflow.com/questions/50582422/ionic-3-show-loading-symbol-on-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!