Modal is a temporary UI that is displayed on top of your current page. This is often used for login, signup, editing existing options and selecting options.
Let us look in to a simple example with modals used. To begin with we are creating an ionic blank project. Let us create a simple modal displaying a message and exit on button click. To do that first we are creating view for our modal.
Message.html
<ion-header>
<ion-toolbar>
<ion-title>
Modal
</ion-title>
<ion-buttons start>
<button (click)="dismiss()">
<span primary showWhen="ios">Cancel</span>
<ion-icon name="md-close" showWhen="android,windows"></ion-icon>
</button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content padding>
<h1>Modal Without Params is created successfully.</h1>
<button full (click)="dismiss()"> Exit </button>
</ion-content>
Message.ts
import { Component } from '@angular/core';
import { ViewController } from 'ionic-angular';
@Component({
templateUrl: 'build/pages/message/message.html',
})
export class MessagePage {
viewCtrl;
constructor(viewCtrl: ViewController) {
this.viewCtrl = viewCtrl;
}
dismiss(){
this.viewCtrl.dismiss();
}
}
This modal displays a message. The modal can be closed or “dismissed” by using the View controllers dismiss method.
Home.html
<ion-header>
<ion-navbar>
<ion-title>
Modal Example
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button full (click)="openModal()">ModalWithoutParams-Message</button>
</ion-content>
Home.ts
import { Component } from '@angular/core';
import { ModalController } from 'ionic-angular';
import {MessagePage} from '../message/message';
@Component({
templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
modalCtrl;
data;
constructor(modalCtrl: ModalController) {
this.modalCtrl = modalCtrl;
this.data = [{name: "aaa", email: "[email protected]", mobile: "1234567890", nickname: "zzz"},
{name: "bbb", email: "[email protected]", mobile: "1234567890", nickname: "yyy"},
{name: "ccc", email: "[email protected]", mobile: "1234567890", nickname: "xxx"}]
}
openModal() {
let myModal = this.modalCtrl.create(MessagePage);
myModal.present();
}
}
Now we are creating our home page importing the ModalController and our data model MessagePage. ModalController’s create method creates modal for our data model MessagePage that is saved to control variable myModal. Present method opens the modal on top of our current page.