You have to use Service for that to implement
Ionic 4: "Loading Controller" dismiss() is called before present() which will keep spinner without dismissing
import { Injectable } from '@angular/core';
import { LoadingController } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class MyLoadingService {
isLoading = false;
constructor(public loadingController: LoadingController) { }
async presentLoading() {
this.isLoading = true;
return await this.loadingController.create({
duration: 5000,
}).then(a => {
a.present().then(() => {
console.log('presented');
if (!this.isLoading) {
a.dismiss().then(() => console.log('abort presenting'));
}
});
});
}
async dismissLoading() {
this.isLoading = false;
return await this.loadingController.dismiss().then(() => console.log('dismissed'));
}
}
in your class you can implement
constructor(public myLoadingService: MyLoadingService, private someService: SomeService)
ngOnInit() {
this.myLoadingService.present();
this.someService.getCustomer('1')
.subscribe(
customer => {
this.customer = customer;
this.myLoadingService.dismiss();
},
error => {
console.log(error);
this.myLoadingService.dismiss();
}
);