我有这个alertCtrl,我想将其用作服务,我的想法是向该警报,标题,文本和一个函数发送3个参数,当用户按下“确定”按钮时,将触发此函数,发送这样的参数,但这行不通。
这是我的代码:
async showMessage(header,message, function){
let alert = await this.alertCtrl.create(
{
header: header,
message: message,
buttons: [
{
text: 'Cancel'
},
{
text: 'Ok',
handler: () => {
function // i want to fire a function here
}
}
]
});
await alert.present();
}
提前谢谢。
问候
答案 0 :(得分:0)
您未在处理程序内部执行该函数,请尝试添加()像
function();
答案 1 :(得分:0)
首先,单词function
是一个关键字,除非声明函数,否则请勿使用。在下面的修改后的代码中,我使用了onOkClick
作为回调函数,您需要将其附加到处理程序。
其次,您不必等待2次,除非您发生了2个独立的并行事件需要合并。即使在那种情况下,您也将需要Promise.all
之类的东西,在其中等待2个异步事件完成,然后将它们解析为一个单独的实体。 More info
尝试下面的代码,它应该可以工作。
async showMessage(header, message, onOkClick) {
let alert = this.alertCtrl.create({
header: header,
message: message,
buttons: [{
text: 'Cancel'
}, {
text: 'Ok',
handler: onOkClick // attach callback
];
});
}
await alert.present();
}