从Ionic2中的模态检索数据

时间:2016-09-20 11:03:00

标签: typescript

我有一个页面,其中有一个按钮,一旦点击就会创建一个模态。

现在我想在我的模态中设置一个变量,并在模态关闭后将其传回页面。

这就是我尝试的方式(我删除了样板代码):

modal.ts

myVar:string;
    constructor(private navCtrl: NavController, private viewCtrl: ViewController) {
    this.myVar = "Hello";

}
// when modal is being closed
onCancel(){
        this.viewCtrl.dismiss(this.myVar);
    }

pageWithButton.ts

varFromModal:string;
    constructor(private navCtrl: NavController, private viewCtrl: ViewController) {}

  // I get this error here: Error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
    this.viewCtrl.onDismiss(data => { 
            this.varFromModal = data;
        });

1 个答案:

答案 0 :(得分:0)

您无法直接在班级中编写代码:

class YourClassName {

    varFromModal:string;
    constructor(private navCtrl: NavController, private viewCtrl: ViewController) {}

    // YOU CANT WRITE CODE HERE !!
    // PUT IT IN A FUNCTION OR CONSTRUCTOR OR WHEREVER ..
    this.viewCtrl.onDismiss(data => { 
        this.varFromModal = data;
    });
}