Ionic modals passing and receiving data

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Great article, as always! I love that you always include a preview and code snippets! 👍
Thanks Catalin, I think the previews and code snippets help a lot that's what I'm always looking for when scrolling through articles.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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:

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() {
console.log(this.number);
}
}
That is how easy it is to pass data to our modal component.
Of course, we also want to be able to receive this data back in our main component (tab1.page.ts).
Before we pass the data back, let's add a function that can modify the number for us.
In our detail.page.html we add the following markup:
<ion-item>
<ion-icon name="remove-circle" (click)="sub()" item-right></ion-icon>
<ion-input
type="number"
min="1"
class="ion-text-center"
[value]="number"
[(ngModel)]="number"
></ion-input>
<ion-icon name="add-circle" (click)="plus()" item-right></ion-icon>
</ion-item>
Now let's add the plus and sub functions in the detail.page.ts file:
plus() {
this.number++;
}
sub() {
this.number--;
}
This will modify our number, but we still need to pass it back to our initial tab1.page. For that, we need to modify the dismiss function.
dismiss() {
this.modalController.dismiss({
number: this.number,
});
}
This will send the number as the variable number.
In our tab1.page.ts we can receive this, but adding a onDidDismiss callback in the presentModal function:
modal.onDidDismiss().then(data => {
this.number = data.data.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 my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter