I am developing an app and I have the next class in TypeScript.
I want to try to get the data in a method of type callback in ´ModalController´ when it dismiss the modal:
import { Component, OnInit } from '@angular/core';
import {NavController, ModalController} from '@ionic/angular';
import { Router } from '@angular/router';
import { HomePage } from '../home/home.page';
import {AddItemPage} from '../add-item/add-item.page';
@Component({
selector: 'app-todo',
templateUrl: './todo.component.html',
styleUrls: ['./todo.component.scss']
})
export class TodoComponent implements OnInit {
public items;
constructor(public navCtrl: NavController, public modalCtrl: ModalController) {
this.ionViewDidLoad();
}
ngOnInit() {}
async addItem() {
// Create a modal using MyModalComponent with some initial data
const modal = await this.modalCtrl.create({
component: AddItemPage, componentProps: {
'prop1': "cadena!!!!!!!"
}
}).then(function(modal) {
return modal.present();
});
modal.onDidDismiss(() => {
// Call the method to do whatever in your home.ts
console.log('Modal closed');
});
}
}
I got this error:
core.js:15724 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'onDidDismiss' of undefinedTypeError: Cannot read property 'onDidDismiss' of undefine
How is related in this post:https://medium.com/@david.dalbusco/how-to-declare-and-use-modals-in-ionic-v4-4d3f42ac30a3 My error was just How I was creating the modal. I have followed this code to create my modal property.
async openModal() {
const modal: HTMLIonModalElement =
await this.modalController.create({
component: DatePickerModal,
componentProps: {
aParameter: true,
otherParameter: new Date()
}
});
modal.onDidDismiss().then((detail: OverlayEventDetail) => {
if (detail !== null) {
console.log('The result:', detail.data);
}
});
await modal.present();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With