×

Ionic Loading

The ionic loader is an overlay that can be used to indicate activity, the user interaction while blocking. The ionic loader shows on the top of the app content, and it can be dismissed by the app to resume when the user interacts with the application. By default, it displays a spinner based on the setting mode. Dynamic content can be passed and shows with the loading spinner. It can include an optional backdrop, which can be hidden by the setting ShowBackdrop: false upon the creation.

The ionic loading has two methods, i.e., Create and Dismiss

Create

The Loading controller can create loading indicators. By loading controller’s create() method, it can be customized by passing loading options. Any other optional HTML can be passed in the content property, and the spinner also is defined in the spinner property. If the value or argument is not passed to a spinner, the loading indicator will use the spinner specified by the platform. To set the spinner name in the ionic application, you are required to set the value of loadingspinner in your app’s configuration. To disable the spinner, you can set the loadspinner: ‘hide’ in the app’s configuration or specify spinner: ‘hide’ in the loading options.

Dismiss

This Dismiss method is used to hide the loader. The loading indicator can be dismissed automatically after a specific amount of time by passing the duration of the loading options. The loading indicator will display during the page changes by default, but it can hide by the setting dismissOnPageChange for true value. The loading indicator dismisses after its creation, and then you need to call the dismiss () method on the loading instance. It can also call the onDidDismiss function to act after the loading indicator is dismissed.

Example: This example explains how you can use the dismiss() or create() method in the ionic loader.

showHideLoader() {  
     this.loadingCtrl.create({  
       message: 'This Loader Will Auto Hide in four Seconds',  
       duration: 4000  
     }).then((res) => {   
       res.present();  
     res.onDidDismiss().then((dis) => {  
       console.log('Loading dismissed! after four Seconds');  
     });  
   });  
 }    

In some cases, you may require a different show and hide methods like as in the server API call. You can see all the methods in the given example.

 showLoader()
{  
     this.loaderToShow = this.loadingCtrl.create({  
       message: 'Loader will Not Hide'  
     }).then((res) => {   
       res.present();  
       res.onDidDismiss().then((dis) => {  
         console.log('Loading dismissed!');  
       });  
     });   
     this.hideLoader();  
   }  
   hideLoader() {  
     setTimeout(() => {   
       this.loadingCtrl.dismiss();  
     }, 5000);   
   }  
 }   

There are some loading options parameters that are as follows:

Options Type Description
spinner string It is used to change the default spinner style.
Content string It is used to load HTML content as a loading indicator.
cssClass string Additional classes to apply for custom styles. When the multiple classes are provided, they should be separated by spaces.
showBackdrop boolean If it is true, the backdrop will be shown behind the loading indicator.
BackdropDismiss boolean If it is true, the loading indicator will be dismissed when the backdrop clicked.
Duration   number It is used to give the time duration in a millisecond to auto-hide the loader.
Message   String It is used to optimal text content to show in the loading indicator.

Example: This example explains how you can use the loading component in the ionic application.

Home.page.html

This file is providing the user interface, and here you create the events to represent the loading controller. For doing this, you are required to create a button inside the <ion-content> component, when the user clicks on that button in response, you will display the loading controller.

When the user clicks on the button, it will call the showLoading() function. This function provides a code to display the loading controller's time limit. When the time completes, which was set by the user, then the loading controller will be dismissed.

<ion-header>  
   <ion-toolbar color="primary">  
     <ion-title>Ionic Loading Example</ion-title>  
   </ion-toolbar>  
 </ion-header>   
 <ion-content class="ion-padding" color="light">  
   <ion-button expand="block" color="success" (click)="showLoading()">Loading</ion-button>  
 </ion-content>   

Home.page.ts

If you want to use a loading component, then you have to import the loading controller. To call the loading controller, you are required to create a constructor inside the class. Create a showLoading() function to show the loading controller to the user.

To dismiss the loading controller, you need to create a dismiss() function. If you want to set the time duration for dismissing the loading controller, then you have to create the setTimeout() function.

import { Component } from
'@angular/core';  
 import { LoadingController } from '@ionic/angular';  
 @Component({  
   selector: 'app-home',  
   templateUrl: 'home.page.html',   
   styleUrls: ['home.page.scss'],  
 })  
 export class HomePage {  
   loaderToShow: any;   
   constructor(public loadingCtrl: LoadingController) {}  
 showLoading() {  
   this.loadingCtrl.create({  
     message: 'Loading.....'   
     }).then((loading) => {  
      loading.present();{
     } 
      setTimeout(() => {   
        loading.dismiss();  
      }, 4000 );   
     });  
   }  
 }   

Output: After executing this code, you will get the output, which is shown below:

Ionic Loading

When you click on the loading button, it will start showing the loading indications, as shown below:

Ionic Loading2

By using the await and async, you can write the show and hide function in another way. The following code explains how you can use it in another way.

By using Async and Await:

export class HomePage {  
   loaderToShow: any;  
   constructor(public loadingCtrl: LoadingController) {}  
   async showLoading() {  
     const loading = await this.loadingCtrl.create({  
     message: 'Loading.....'   
     });  
     loading.present();  
     setTimeout(() => {  
       loading.dismiss();  
     }, 8000 );  
  }   
 } 

Related Topics

Ionic Camera

Hybrid mobile applications can make very quickly, but sometimes it becomes a challenge to get the features that access the hardware such as GPS, storage, file storage, cameras, and networks. Cordova offers plugins...

4 minutes read.

Ionic Date Time

Ionic Date Time The DateTime component is used to display an interface that makes it simple for users to select dates and times. Tapping on <ion-datetime> will display a slider up from the...

4 minutes read.

Ionic Tutorial for Beginners

What is Ionic? Ionic is an open-source framework that is used for the development of mobile applications. It is also used to build android applications, iOS, applications, desktop applications, and web applications using web...

4 minutes read.

Ionic Alert

Ionic Alert An Alert is a dialog box that presents the users with important information, or it stores the data from the user by using the inputs. Alerts are a great way to provide...

5 minutes read.

Ionic Infinite Scroll

The infinite scroll permits you to perform an action when the user scrolls a particular content from the bottom or top of the page. When you display the large collection of data...

3 minutes read.

Ionic vs React Native

Ionic Vs React Native Ionic and React Native are both common framework which is used to develop mobile applications. Here, you will know about the popular differences between the ionic and the React Native. Ionic Ionic...

3 minutes read.

Ionic Modal

Ionic modal is displayed as a temporary UI that slides into the screen. The ionic modal is a dialog box that is displayed at the top of the app’s content, and it...

5 minutes read.

Ionic Input

Ionic Input Inputs are necessary to collect and process user inputs securely. To be easy for users to communicate, they must follow the process and communication guidelines for each platform. It is a...

3 minutes read.

Ionic Range

The Range slider lets users choose from a range of values by moving the slider knob. You can accept two knobs, but by default, one-knob controls the range value. Range labels You can put down on...

2 minutes read.

Ionic vs PhoneGap

Ionic Vs PhoneGap Ionic and PhoneGap are both used to develop the mobile application. Both Frameworks are used in large enterprises by the developers. Here, we are going to compare the important differences between ionic...

3 minutes read.

Ionic Slides

The ionic slides component is the multi-section container. You can swipe or drag any segment between them. This contains any number of slides components. Slides help it easy to make galleries, tutorials, and...

2 minutes read.

Ionic Toggle

The toggle is the same as the HTML checkbox entry, but it looks different and easier to use on a touchscreen device. It is an input component that holds the boolean value. Toggle...

2 minutes read.

Ionic Buttons

Ionic Buttons There are different types of buttons in the Ionic framework. These buttons are subtly animated, enhancing the user experience when using the application. Buttons are a fundamental way of interacting and navigating...

4 minutes read.

Ionic Toolbar

A toolbar is a general toolbar that can be used in an application such as a header, sub-header, footer, or even a sub-footer. Because the ion-toolbar is flexbox-dependent, no matter how many toolbars...

3 minutes read.

Ionic Splash Screen

The Apache Cordova plugin helps to displays and hides a splash screen during the application launch. The Ionic Splash Screen is similar to a home page on a website. The ionic splash...

2 minutes read.

Features of Ionic Framework

The Ionic Framework is based on the AngularJS framework that permits a programmer to use a combination of many other programming languages, like HTML5, CSS, and JavaScript. Some of the...

4 minutes read.

Ionic Radio

Ionic Radio The radio button is the type of input component that has a Boolean value. The Ionic radio button has a different pattern on every platform, as well as other Ionic components....

2 minutes read.

Ionic Colors

Ionic Colors Before we start with the actual elements available in the Ionic frame, let's understand a little bit how Ionic uses colors for different elements. Ionic Color Classes Ionic framework is a set of nine predefined...

3 minutes read.

Difference between Ionic 4 and Ionic 3

The ionic framework is an open-source platform that is used to develop mobile applications. It is also used to build android applications, desktop applications, iOS, and web applications using web technologies like HTML,...

5 minutes read.

Ionic Loading

The ionic loader is an overlay that can be used to indicate activity, the user interaction while blocking. The ionic loader shows on the top of the app content, and it can...

5 minutes read.