Ionic 3模态navparams返回undefined

时间:2017-07-21 14:43:36

标签: angular typescript ionic2 ionic3

所以我试图将一个字符串作为参数传递给一个Modal(具体是master / detail),而Modal确实打开了,但字符串返回undefined。

这是父母ts:

 editReminder(event, data){
    console.log(data);
    let myModal = this.modalCtrl.create(ReminderDetailsPage, data);
    myModal.present();
}

HTML正在使用ngFor(提醒提醒)显示提醒列表。这个方法在html上获取这样的字符串:

 <button ion-fab mini class="mini-button" (click)="editReminder($event, reminder)">
          <ion-icon name="create"></ion-icon>
 </button>

如果我是console.log(数据),它会像平常一样返回提醒。但是当我尝试在ReminderDetails模式上获取它时:

export class ReminderDetailsPage {

   EditedReminder: string = this.navParams.get('data');

它只是返回undefined。我在两个页面上都导入了navParams,并在构造函数中声明了它们。

3 个答案:

答案 0 :(得分:2)

create()

将对象传递给navParams.get() - 方法。 并在constructorngOnInit等生命周期函数中调用X.Msg.Confirm("Confirm", "The field has " + dependency.Count() + " dependent fields. Are you sure you want to proceed? (The dependent fields will be deleted also)", new MessageBoxButtonsConfig { Yes = new MessageBoxButtonConfig { Handler = "App.direct.UC.DoYesDeleteDepField('" + fieldname + "," + dependency + "')", //ERROR LINE Text = "Yes" }, No = new MessageBoxButtonConfig { Handler = "", Text = "No" } }).Show();

答案 1 :(得分:0)

在模态页面

ionViewWillLoad() {
 const data = this.navParams.get('data');
 console.log(data);
}

如果你想将数据传回模态..

import {NavParams, ViewController} from 'ionic-angular';
...
    data: String
     constructor(private navParams: NavParams, private view: ViewController) {}
    ionViewWillLoad() {
     this.data = this.navParams.get('data');
     console.log(this.data);
    }

  closeModal() {
    this.view.dismiss(data);
  }

然后在调用页面:

 const myModal: Modal = this.model.create('ModalPage', {data: myData})
 myModal.present();
 myModal.onWillDimiss(data) => {
     console.log(`About to dismiss with: ${data}`);
 })
 myModal.onDidDimiss(data) => {
     console.log(`Dismissed with: ${data}`);
 })

答案 2 :(得分:0)

在这种情况下,我建议您使用存储而不是使用NavParams