Ionic modals passing and receiving data

Ionic modals passing and receiving data

Chris Bongers
·Mar 4, 2021·

3 min read

Listen to this article

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 end result for today will look something like this:

Ionic modal passing and receiving data

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 modify the dismiss function.

dismiss() {
      number: this.number,

This will send the number as the variable number.

In our we can receive this, but adding a 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 will 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

Did you find this article valuable?

Support Chris Bongers by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this