angular - 如果出现错误,请保留警告框而不关闭
问题描述
这只是一个带有单选按钮的警报框。除了一件事,一切都很好。也就是说,如果出现错误,我无法保留警报框,直到用户输入正确的数据。根据文档,我使用了return false
功能。但还没有运气。有什么线索吗?
Note:
如果输入是text boxes
. 在这里我需要单选按钮。
const allApiKeys = await this.apiKeySqliteProvider.getAllApiKeys();
const alert = this.alertCtrl.create();
alert.setTitle('Select Api Key');
forEach(allApiKeys, (apiKey: ApiKey) => {
alert.addInput({
type: 'radio',
label: apiKey.name,
value: apiKey.key,
checked: false
});
});
alert.addButton({
text: 'Cancel',
role: 'cancel',
handler: data => {
this.loading.dismissLoader(loading);
}
});
alert.addButton({
text: 'OK',
handler: data => {
let navTransition = alert.dismiss();
navTransition.then(() => {
if (data == null) {
this.showToastProvider.showErrorToast("Invalid API Key");
this.loading.dismissLoader(loading);
return false;
}
});
return false;
}
});
alert.present();
}
解决方案
return false
不是实际问题。你在打电话alert.dismiss
,这就是问题所在。如果您不想隐藏警报,则应将dismiss
代码移动到else
块内。
请将您的代码更改为以下
const allApiKeys = await this.apiKeySqliteProvider.getAllApiKeys();
const alert = this.alertCtrl.create();
alert.setTitle('Select Api Key');
forEach(allApiKeys, (apiKey: ApiKey) => {
alert.addInput({
type: 'radio',
label: apiKey.name,
value: apiKey.key,
checked: false
});
});
alert.addButton({
text: 'Cancel',
role: 'cancel',
handler: data => {
this.loading.dismissLoader(loading);
}
});
alert.addButton({
text: 'OK',
handler: data => {
if (data == null) {
this.showToastProvider.showErrorToast("Invalid API Key");
this.loading.dismissLoader(loading);
return false;
}
// you don't need else here as by default it will hide alert
}
});
alert.present();
}
推荐阅读
- c++ - 在函数 C++ 中调用模板函数
- json - WebHook Azure 数据工厂传递变量
- c++ - 在另一个之上创建一个弹出窗口
- django - ModelMultipleChoiceField 字段无返回值
- android-room - Android Jetpack Room 一对多查询哪里
- javascript - 将 PHP 结合到 Javascript 中?
- python - Asyncio aiohttp - 每秒发出的并行请求
- c++ - 仅在所有输入 c++ 的末尾打印
- asp.net-core - 使用 Swashbuckle 生成的 OpenAPI 规范包含每个参数的默认空值
- javascript - 使用react点击删除删除相应的div