Ionic framework IONIC中的连续确认警报
我正试图从用户那里得到两个又一个的确认。如果第一个被取消,则无需获得第二个。但如果第一个是一致的,那么第二个应该显示和处理。我使用的是这样一段代码Ionic framework IONIC中的连续确认警报,ionic-framework,ionic3,Ionic Framework,Ionic3,我正试图从用户那里得到两个又一个的确认。如果第一个被取消,则无需获得第二个。但如果第一个是一致的,那么第二个应该显示和处理。我使用的是这样一段代码 showConfirmation1() { let confirm = this.alertCtrl.create({ title: 'C1', message: "M1", buttons: [ { text: 'No', }, { text: 'Yes
showConfirmation1() {
let confirm = this.alertCtrl.create({
title: 'C1',
message: "M1",
buttons: [
{
text: 'No',
},
{
text: 'Yes',
handler: data => {
this.showConfirmation2();
}
}
]
});
showConfirmation2() {
let confirm = this.alertCtrl.create({
title: 'C2',
message: "M2",
buttons: [
{
text: 'No',
},
{
text: 'Yes',
handler: data => {
this.doYourJob();
}
}
]
});
如果用户取消第一个,一切正常。如果用户接受第一个,则正确显示第二个。然而,第二个有点僵硬。它的“否”按钮和“是”按钮都不起作用。当您单击页面的其他区域时,它甚至不会消失。更新:
找到了一个更干净的解决方案,它需要更少的代码,更容易理解。
通过在按钮
handler
中返回false
,可以阻止警报关闭。并且discouse
-函数返回一个在转换完成时得到解决的问题。这就是我们调用函数打开第二个警报的地方
showConfirmation1() {
let confirm = this.alertCtrl.create({
title: 'C1',
message: "M1",
buttons: [
{
text: 'No',
},
{
text: 'Yes',
handler: data => {
confirm.dismiss()
.then(() => {
this.showConfirmation2();
});
return false;
}
}
]
});
confirm.present();
}
原件:
在第一次发出警报时,请使用解除(数据:任意)
功能。并传递一个值,该值指示未取消。在ondiddisclesh
-函数中检查此值。如果有,则显示第二个警报
showConfirmation1() {
let confirm = this.alertCtrl.create({
title: 'C1',
message: "M1",
buttons: [
{
text: 'No',
},
{
text: 'Yes',
handler: data => {
confirm.dismiss({ showSecond: true });
return false;
}
}
]
});
confirm.onDidDismiss(data => {
if(data && data.showSecond){
this.showConfirmation2();
}
}
confirm.present();
}