Ionic modals passing and receiving data


How to pass and receive data from modals in Ionic

4 Mar, 2021 · 2 min read

Yesterday we made a modal in Ionic, modals are really cool, but generally, we want to pass or receive data from them.

That will be what we’ll be making today, a modal that passes, modifies, and returns data.

The result for today will look something like this:

Passing data to an Ionic modal

Let’s first start by passing data to our modal. This is as simple as calling the componentProps on our modalController create function.

number: number = 3;

const modal = await this.modalController.create({
  component: DetailPage,
  componentProps: {
    number: this.number,

Then in the DetailPage we can retrieve these values by defining them inside our component!

export class DetailPage implements OnInit {
  number: number;

  ngOnInit() {

That is how easy it is to pass data to our modal component.

Ionic modal dismiss receive data

Of course, we also want to be able to receive this data back in our main component (

Before we pass the data back, let’s add a function that can modify the number for us.

In our, we add the following markup:

  <ion-icon name="remove-circle" (click)="sub()" item-right></ion-icon>
  <ion-icon name="add-circle" (click)="plus()" item-right></ion-icon>

Now let’s add the plus and sub functions in the file:

plus() {

sub() {

This will modify our number, but we still need to pass it back to our initial For that, we need to alter the dismiss function.

dismiss() {
    number: this.number,

This will send the number as the variable number.

In our we can receive this, but adding an onDidDismiss callback in the presentModal function:

modal.onDidDismiss().then((data) => {
  this.number =;

This will receive the data and update the number. Then the next time we open the modal, the new number will reflect.

And there you have it, passing and receiving data in Ionic Modals.

You can find today’s code on GitHub.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

How to solve App Tracking Transparency app store rejection in Ionic

4 Aug, 2021 · 4 min read

How to solve App Tracking Transparency app store rejection in Ionic

Ionic tab bar circle middle button

19 May, 2021 · 2 min read

Ionic tab bar circle middle button

Join 2099 devs and subscribe to my newsletter