How to Pass Data and Receive the Response in Ionic 4 Modals?

How to Pass Data and Receive the Response in Ionic 4 Modals?

In this Ionic 4 tutorial, we are going to learn how to use modal popover in an Ionic 4 Angular application. We will also learn how to pass data and receive the response in Ionic 4 modals

In this Ionic 4 tutorial, we are going to learn how to use modal popover in an Ionic 4 Angular application. We will also learn how to pass data and receive the data response in Ionic 4 modals.

Table of Contents

  • Ionic 4 Environment Setup
  • Create an Ionic 4/Angular Project
  • Passing Data to Modal Page in Ionic 4
  • Create The Modal Popup Page
  • Receive Data in Ionic 4 Modal
  • Dismissing Modal in Ionic 4
  • Showing the Model Popover
  • Conclusion

Ionic 4 Environment Setup

To create Modals in Ionic 4, you must have the latest versions of Node js, Ionic and Cordova installed on our machine.

To get started with Node js you need to download the latest version of Node from here.

sudo npm install -g cordova ionic

To update the Ionic and Cordova to the latest version, use the following command:

sudo npm update -g cordova ionic

Please make sure you must have Xcode set up on your machine. We are using Visual Studio Code in this tutorial; however, you can use any text editor of your choice. Now we are all set to create a brand new Ionic project, follow the next step.

Create an Ionic 4/Angular Project

To build modals in an Ionic 4 app we need to generate basic Ionic/Angular project from scratch. Run the below command to create a brand new Ionic 4/Angular project.

ionic start ionic-modals-app --type=angular

Let’s pick the perfect starter template! 💪

Starter templates are ready-to-go Ionic apps that come packed with everything you need to build your app. To bypass this prompt next time, supply template, the second argument to ionic start.

? Starter template: 
  tabs         | A starting project with a simple tabbed interface 
  sidemenu     | A starting project with a side menu with navigation in the content area 
❯ blank        | A blank starter project 
  my-first-app | An example application that builds a camera with gallery 
  conference   | A kitchen-sink application that shows off all Ionic has to offer

Let’s head over to the ionic modals app’s project directory.

cd ionic-modals-app

Now, run the Ionic 4 app using the below commands.

ionic serve

ng run app:serve --host=localhost --port=8100

Now, your project will open on http://localhost:8100/home URL, you can view your Ionic/Angular project in the browser’s address bar.

Passing Data to Modal Page in Ionic 4

Let’s pass data to modal in Ionic 4 using modalController.create() method. To initiate the modal popover in Ionic add the following code inside the src/app/home/home.page.ts file.

import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { ModalPopupPage } from '../modal-popup/modal-popup.page';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})

export class HomePage {

  constructor(public modalController: ModalController) { }

  async showModal() {
    const modal = await this.modalController.create({
      component: ModalPopupPage,
      componentProps: {
        'name': 'Hello User'
      }
    });
    return await modal.present();
  }

}

Import the ModalController from ‘@ionic/angular’, this module helps in calling the regular pages in Ionic 4. Then, Inject ModalController in the constructor. Use the .create() method to pass the data to the modal popover in the showModal() function.

Create The Modal Popup Page

In this step, we will create a regular modal popup page in the Ionic 4/Angular project. Enter the below command in your terminal.

ng generate page ModalPopup

Import ModalPopup page in app.module.ts and inject inside the declarations and entryComponents array.

import { ModalPopupPage } from './modal-popup/modal-popup.page';

@NgModule({
  declarations: [ModalPopupPage],
  entryComponents: [ModalPopupPage],
  imports: [...],
  providers: [...],
  bootstrap: [...]
})

export class AppModule { }

Receive Data in Ionic 4 Modal

To get the data in Ionic 4, we need to import the NavParams in the ModalPopupPage and inject it inside the constructor(), use navParams.get() method to receive the data response in the browser’s console.

Go to src/app/modal-popup.page.ts and add the following code in it.

import { Component, Input } from '@angular/core';
import { NavParams } from '@ionic/angular';

@Component({
  selector: 'app-modal-popup',
  templateUrl: './modal-popup.page.html',
  styleUrls: ['./modal-popup.page.scss'],
})

export class ModalPopupPage {
  @Input() name: string;

  constructor(
    public navParams: NavParams
  ) {
    console.log(navParams.get('name'));
  }
}

Dismissing Modal in Ionic 4

To terminate the modal, import and inject ModalController and define the closeModal() method and bind it to ion-button HTML tag.

Final src/app/modal-popup/modal-popup.page.ts file.

import { Component, Input } from '@angular/core';
import { NavParams, ModalController } from '@ionic/angular';

@Component({
  selector: 'app-modal-popup',
  templateUrl: './modal-popup.page.html',
  styleUrls: ['./modal-popup.page.scss'],
})

export class ModalPopupPage {
  @Input() name: string;

  constructor(
    public navParams: NavParams,
    public modalCtrl: ModalController
  ) {
    console.log(navParams.get('name'));
  }

  public closeModal() {
    this.modalCtrl.dismiss({
      'dismissed': true
    });
  }
}

Call the closeModal() method in HTML template.

<ion-button ion-button (click)="closeModal()">Close</ion-button>

Here, is the full HTML code for Ionic modal template. Add the code inside the modal-popup.page.html file.

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-button ion-button (click)="closeModal()">Close</ion-button>
    </ion-buttons>
    <ion-title>Title</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>

</ion-content>

Showing the Model Popover

To show the model we need to add the click event and pass the showmodal() function in it. Head over to home.page.html template and add the following code inside of it.

<ion-button color="primary" (click)="showModal()">Show Modal</ion-button>

Conclusion

Finally, we have completed the Ionic 4/Angular Modals tutorial. In this tutorial, we learned how to send data to the modal component page vice versa receive data responses on the modal page. Get the complete source code of this tutorial on my Git repo.

Ionic Angular

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

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

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. 

Ionic 5 Tutorial: Create Offline Price Checker (Angular 9)

In this Ionic 5 tutorial, we will create an offline price checker using SQLite, Barcode Scanner, and Angular 9. The price checker usually uses by the store or retail business to check the actual price of the item, product, or goods. So, inside this app will contain an SQLite database table of products that have fields id, price look-up (PLU) or product code, barcode, product name, product description, and product sell price.

Angular 9, Ionic 5, React Navigation 5 - Web Dev Update February 2020

Here's an update on what's important in web development! Angular 9, Ionic 5, React Navigation 5 and more - I'll bring you up-to-date with what you need to know!

Basics of Angular: Part-1

What is Angular? What it does? How we implement it in a project? So, here are some basics of angular to let you learn more about angular. Angular is a Typesc