×

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 will disappear after the user interaction. The modal is useful as a select component. When there are many options to select from or when filtering items in a list. Such cases are that login or signup, composing messages, and presenting app configuration options.

Ionic Modal Controller

The modal controller programmatically controls the modal component. Ionic modal can be created or dismissed from the modal controller. By using the create() and dismiss() method, you can create and dismiss the ionic modal easily.

There are two methods of modal which are used in the ionic application that are as follows:

  1. Create
  2. Dismiss

Create

The create() method is used to create the modal. It creates a modal overlay with the different modal options. By setting modal options in the create method, you can customize the controller.

Dismiss

The dismiss() method is used to dismiss the modal overlay after creating the modal. For performing the additional actions after the model is dismissed, then you can call the onDidDismiss function.

Now, you can see how the modal controller works in the ionic application. You can use these steps to create the modal that are given below:

Step-1: Firstly, you create a new project. To creating a new project, you can select the blank app template to create a home page in the ionic.

Step-2: Create a new page for the modal controller. For creating a modal controller in the ionic application, you are required to run the given below command before running the ionic serve command.

$ ionic g page modal

Step-3: After creating the model page in the ionic application, you open the following files and add the provided code. You can also modify the code according to your requirements.

Example: This example explains how you can work with the modal controller in the ionic application.

Modal.page.html

<ion-header>  
   <ion-toolbar color="success">  
     <ion-title>Ionic Modal</ion-title>  
     <ion-icon name='close-circle' color="dark" (click)='dismiss()' style='float: right;'></ion-icon>  
   </ion-toolbar>  
 </ion-header>   
 <ion-content color="light">  
   <ion-list>  
     <ion-item>  
       <ion-avatar slot="start">   
         <img src="assets\icon\avatar.png"/>  
       </ion-avatar>  
       <ion-label>   
         <h2>Java</h2>  
         <p>Java is the Object oriented programming language.</p>  
       </ion-label>  
     </ion-item>   
     <ion-item>  
       <ion-avatar slot="start">  
         <img src="assets\icon\avatar.png"/>  
       </ion-avatar>  
       <ion-label>  
         <h2>Python</h2>   
         <p>Python is an interpreted language.</p>  
       </ion-label>  
     </ion-item>  
     <ion-item>   
       <ion-avatar slot="start">  
         <img src="assets\icon\avatar.png"/>  
       </ion-avatar>  
       <ion-label>  
         <h2>C Programming</h2>  
         <p>C is a structured programming language.</p>  
       </ion-label>  
     </ion-item>  
     <ion-item>
       <ion-avatar slot="start"> 
         <img src="assets\icon\avatar.png">
       </ion-avatar>
       <ion-label>
         <h2>JavaScript</h2> 
         <p>JavaScript is the programming language of HTML and the web.</p>
       </ion-label>
     </ion-item>
   </ion-list>   
 </ion-content> 

Modal.page.ts

Here, you have to create a dismiss() method to dismiss the modal user interface.

import { Component, OnInit } from '@angular/core'; 
 import { ModalController} from '@ionic/angular';  
 @Component({  
   selector: 'app-modal',  
   templateUrl: './modal.page.html',   
   styleUrls: ['./modal.page.scss'],  
 })  
 export class ModalPage implements OnInit {  
   constructor(public modalCtrl: ModalController) {}  
   ngOnInit() {  
   }  
   dismiss() {  
     this.modalCtrl.dismiss();  
   }  
 } 

Step-4: In this step, you have to construct the app.module.ts file. Here, you can import the modal page and then include it inside the @NgModule.

App.module.ts

 import { NgModule } from '@angular/core';  
 import { BrowserModule } from '@angular/platform-browser';  
 import { RouteReuseStrategy } from '@angular/router';  
 import { IonicModule, IonicRouteStrategy } from '@ionic/angular';  
 import { SplashScreen } from '@ionic-native/splash-screen/ngx';  
 import { StatusBar } from '@ionic-native/status-bar/ngx';  
 import { AppComponent } from './app.component';   
 import { AppRoutingModule } from './app-routing.module';  
 import { ModalPageModule } from './modal/modal.module';  
 @NgModule({  
   declarations: [AppComponent],  
   entryComponents: [],  
   imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, ModalPageModule],   
   providers: [  
     StatusBar,  
     SplashScreen,  
     { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }  
   ],  
   bootstrap: [AppComponent]   
 })  
 export class AppModule {}    

Step-5: In this step, you open the Home.page.html file to write the html code for the ionic application.

Home.page.html

<ion-header>  
   <ion-toolbar color="primary">  
     <ion-title>Ionic Modal Example</ion-title>  
   </ion-toolbar>  
 </ion-header>   
 <ion-content class="ion-padding" color="light">  
   <h1>Ionic Modal Controller</h1>  
   <ion-button expand="round" color="success" (click)="showModal()">  
     Show Modal 
   </ion-button>  
 </ion-content>   

Step-6: In this step, you open the home.page.ts file and write the ts code to the ionic application. The showModel() method is used to create the modal cotroller.

Home.page.ts

import { Component } from '@angular/core';  
 import { ModalController} from '@ionic/angular';  
 import { ModalPage } from '../modal/modal.page';  
 @Component({  
   selector: 'app-home',   
   templateUrl: 'home.page.html',  
   styleUrls: ['home.page.scss'],  
 })  
 export class HomePage {  
   constructor(public modalCtrl: ModalController) {}   
   async showModal() {  
     const modal = await this.modalCtrl.create({  
       component: ModalPage  
     });  
     return await modal.present();   
   }  
 }   

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

Ionic Modal

When you click on the show modal button, then it opens the new page on the mobile screen. If you want to close that page, then you click on the close button which appears at the bottom of the toolbar, as shown below:

Ionic Modal2

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 Segment

A segment is a collection of buttons, sometimes which are known as segmented controls. The segment buttons are displayed at horizontal rows. These segment buttons can be shown in a toolbar or the...

2 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 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 Select

The Ion Selection component is similar to the HTML <select> element. However, the Ionic Selection component makes it easy for users to categorize and choose the preferred option or other options. You must...

3 minutes read.

Ionic Navigation and Routing

The latest version (ionic 4) of the ionic framework, no longer limited to the angular. In Ionic 4, you can create an ionic project in different front-end frameworks such as Vue.js, React.js, and...

6 minutes read.

Ionic Refresher

Refresher means updating the currently showing page so that the user can see the latest view. It is also known as re-loading in web technology. It provides a pull- to-refresh feature on a...

4 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.

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 Editors

Editors are software programs that permit the users to create and edit the text files. In the development field, editors are usually used to create the source code editors that includes many specific...

3 minutes read.

Ionic List

Ionic List The ionic list is created of several rows of items that can contain text, buttons, thumbnails, keys, icons, and more. Lists are one of the essential components in any web or...

4 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 Menu

The Ionic menu is the side-menu navigation that can be slide in from the side of the current view. By default, the menu slides in from the left side, but the side...

7 minutes read.

Ionic Popover

The Ionic popover is a small dialog box that displays at the top of the screen. It can be used for anything, but it is usually used for a button, an action,...

4 minutes read.

Ionic Actionsheet

Ionic Actionsheet The Action sheet is a dialog box that displays a group of options. It is shown on the top of the application content and must be dismissed manually before the user can resume...

3 minutes read.

Ionic-fab Button

Ionic-fab Button The Ionic FABs (Floating Action Buttons) are standard material design compounds. It is a container element that contains one or more fab buttons. They are required to be placed in a fixed position,...

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 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 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 Tabs

The ionic tabs mostly used for mobile navigation. It means that tabs are placed at the top of the display on android devices, while at the bottom on IOS. It does not provide...

2 minutes read.