Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'onDidDismiss' does not exist on type ionic 4 and Angular

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

like image 407
Fernando Pie Avatar asked Oct 18 '25 12:10

Fernando Pie


1 Answers

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();
}
like image 148
Fernando Pie Avatar answered Oct 21 '25 02:10

Fernando Pie