我对 Typescript
相当陌生,我在提供程序中有以下代码
...
confirm(message, title=""){
let confirm = this.alertCtrl.create({
title: title || "Please confirm",
message: message,
buttons: [
{
text: 'Yes',
handler: () => {
return true;
}
},
{
text: 'No',
handler: () => {
return false;
}
}
]
});
confirm.present();
}
更新:
页面中有一个按钮,它调用函数deleteItems
deleteItems(){
this.popup.confirm("Are you sure you want to delete this item");
if(this.popup.isTrue == true){
//delete items
}
}
我希望 confirm
方法根据单击的按钮"is"或“否”返回 true 或 false。
谁能告诉我如何实现这一目标?
最佳答案
两个按钮各有一个处理程序。我将使用处理程序来设置a
。
定义箭头函数
a:boolean;
onYesHandler = ()=>{
this.a = true;
}
onNoHandler = () =>{
this.a = false;
}
让confirm
函数获取处理程序和消息:
confirm(message, yesHandler,noHandler,title=""){
let confirm = this.alertCtrl.create({
title: title || "Please confirm",
message: message,
buttons: [
{
text: 'Yes',
handler: yesHandler
},
{
text: 'No',
handler: noHandler
}
]
});
confirm.present();
}
调用confirm
函数:
this.popup.confirm("Are you sure you want to delete this item",this.onYesHandler,this.onNoHandler);
关于angular - ionic2 AlertController,如何从方法返回按钮单击的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165285/