我正在开发一个带模态的Ionic应用程序。当我使用onDidDismiss
方法时,我没有从模态到页面获取值。
我想在模态dissmiss()
时发送数据。
我的代码如下。
home.html的
<ion-content>
<ion-input type="text" maxlength="50" name="name1" placeholder="eg: Software Engineer" (input)="openModalNew();"></ion-input>
</ion-content>
home.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController, ModalOptions, Modal } from 'ionic-angular';
import { NavController, NavParams } from 'ionic-angular';
import { DetailsPage } from '../details/details'
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public modalCtrl: ModalController,public params: NavParams) {
}
openModalNew() {
let MyNewModal = this.modalCtrl.create(DetailsPage);
MyNewModal.onDidDismiss(data => {
console.log(data); // getting as null
});
MyNewModal.present();
}
}
Details.html
<ion-header>
<ion-navbar>
<ion-title>ModalPage</ion-title>
<ion-buttons end>
<button ion-button (click)="closeModal()">Close</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
</ion-content>
details.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ViewController } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-details',
templateUrl: 'details.html',
})
export class DetailsPage {
constructor(public navCtrl: NavController, public navParams: NavParams, public viewCtrl : ViewController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad ModalPage');
}
public closeModal(){
let data = { 'name':'kumar','id':'12345' };
this.viewCtrl.dismiss(data);
}
}
这里onDidDismiss
的值变为null。如何解决问题请帮帮我。我试了好几次。
我通过在app.module.ts的entryComponents中添加模型“DetailsPage”来获得解决方案。
app.module.ts
@NgModule({
declarations: [
DetailsPage
],
entryComponents: [
DetailsPage
],
我在home.ts更改代码
let MyNewModal = this.modalCtrl.create(DetailsPage);
代替
let MyNewModal = this.modalCtrl.create("DetailsPage");