Ionic 4/Angular Modals - Tutorial

Ionic 4/Angular Modals - Tutorial

In this tutorial, we are going to see how to use modals in Ionic 4/Angular apps by creating a simple demo app so let's get started!

First of all, We need to mention that we are using the Ionic CLI 4 so if you didn't yet upgrade to this release make sure to use the equivalent commands for Ionic CLI prior versions.

Generating a New Ionic 4/Angular Project

Before implementing our modal example, let's first generate a brand new Ionic 4 project based on Angular.

Go ahead and open your terminal window then type the following command:

ionic start ionic-modals blank --type=angular

Next, navigate inside your project directory and serve your app using the following command:

cd ionic-modals
ionic serve 

You should be able to visit your app by going to the http://localhost:8100 address.

Importing and using ModalController

Open src/pages/home/home.ts then import ModalController from ionic-angular:

import { ModalController } from 'ionic-angular';

Next we need to inject via component constructor:

selector: 'page-home',
templateUrl: 'home.html'
export class HomePage {
constructor(public navCtrl: NavController,public modalCtrl : ModalController) {



Opening a Modal Page

The next step is to add the openModal() method to the HomePage component and bind it to a button on the home.html file:

public openModal(){


<button ion-button (click)="openModal()">Open Modal</button>

Before adding an implementation for the openModal() method, let's first add a new page to be used by our modal.

Head to your terminal inside your project folder and run the following command:

ionic g page ModalPage

Then in the home.ts file, use the create() method of ModalController to create a modal and then show it:

public openModal(){ var modalPage = this.modalCtrl.create('ModalPage'); modalPage.present(); }

Now if you click on open modal button you should be able to see a modal page popups.

Closing the Modal Page

We have added a method for opening a modal, now let's add a method to close the modal page once it is opened.

Open the src/pages/modal/modal.tsthen add thecloseModal()method to theModalPage` component:

public closeModal(){


Then open the src/pages/modal/modal.html file and add a button to close the modal:


<ion-navbar> <ion-title>ModalPage</ion-title> <ion-buttons end> <button ion-button (click)="closeModal()">Close</button> </ion-buttons> </ion-navbar>


<ion-content padding>


Now let's implement the closeModal() method.

First we need to import ViewController from the ionic-angular package:

import { ViewController } from 'ionic-angular';

Inject ViewController via component constructor:

export class ModalPage {

constructor(public viewCtrl : ViewController ) { }

Next, use the dismiss() method to close the modal:

export class ModalPage {

constructor(public navCtrl: NavController, public viewCtrl : ViewController ,public navParams: NavParams) { } public closeModal(){ this.viewCtrl.dismiss(); }


Passing Data to Modal Page Component

You can also pass some data to a modal page component using the second parameter of the create() method. Passed data can be of any type: string , number or object.

Go ahead, change the openModal() method to pass some data object:

public openModal(){
    var data = { message : 'hello world' };
    var modalPage = this.modalCtrl.create('ModalPage',data);

Now let's get the passed parameters using NavParams:

selector: 'page-modal',
templateUrl: 'modal.html',
export class ModalPage {

constructor(public navCtrl: NavController, public viewCtrl : ViewController ,public navParams: NavParams) { } public closeModal(){ this.viewCtrl.dismiss(); } ionViewDidLoad() { console.log('ionViewDidLoad ModalPage'); console.log(this.navParams.get('message')); }



This is the end of this tutorial. We have seen how to use modals in Ionic 4/Angular.

Originally published  at on 01 Aug 2019


Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Ionic 4, Angular 7 and Cordova Crop and Upload Image

Ionic 4 - Build iOS, Android & Web Apps with Ionic & Angular

Building CRUD Mobile App using Ionic 4, Angular 8

How to Build Mobile Apps with Angular, Ionic 4, and Spring Boot

Ionic 4 JWT Authentication Tutorial: Using Angular HttpClient with Node & Express.js Server

angular ionic web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Wondering how to upgrade your skills in the pandemic? Here's a simple way you can do it.

Corona Virus Pandemic has brought the world to a standstill. Countries are on a major lockdown. Schools, colleges, theatres, gym, clubs, and all other public

Hire Web Developer

Looking for an attractive & user-friendly web developer?, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Pros and Cons of Ionic Development

If you think Ionic is the right cross-platform application development, here are a few pros and cons of Ionic development.