【发布时间】:2018-02-25 07:22:40
【问题描述】:
我有包含超过 12 个选项的单选按钮警报,我想检查等于我的变量值的选项。
我使用 if 语句用少于 4 个选项来做这件事。但是现在我有超过 12 个选项,所以我希望有更简单的方法来检查我的变量的值并选择相等的选项。
---编辑---
这是我的html的一部分
<button ion-button clear full (click)="selectColor(x.color)">{{x.color}}</button>
.ts 函数(我想要一个更好的方法来选择警报的单选按钮(如果有的话))
selectColor(color){
let alert1 = this.alertCtrl.create();
alert1.setTitle('Color Theme');
if(color=="red"){
alert1.addInput({
type: 'radio',
label: 'Red',
value: 'red',
checked: true
});
}else{
alert1.addInput({
type: 'radio',
label: 'Red',
value: 'red',
});
}
if(color=="pink"){
alert1.addInput({
type: 'radio',
label: 'Pink',
value: 'pink',
checked: true
});
}else{
alert1.addInput({
type: 'radio',
label: 'Pink',
value: 'pink'
});
}
if(color=="purple"){
alert1.addInput({
type: 'radio',
label: 'Purple',
value: 'purple',
checked: true
});
}else{
alert1.addInput({
type: 'radio',
label: 'Purple',
value: 'purple'
});
}
if(color=="blue"){
alert1.addInput({
type: 'radio',
label: 'Blue',
value: 'blue',
checked: true
});
}else{
alert1.addInput({
type: 'radio',
label: 'Blue',
value: 'blue'
});
}
...
alert1.addButton('Cancel');
alert1.addButton({
text: 'Okay',
handler: data => {
...
}
});
alert1.present();
}
【问题讨论】:
-
能否将代码添加到帖子中?
-
@sebaferreras 是的,我做到了.. 我花了很长时间检查警报单选按钮,但希望有一个简短的方法可以帮助我,以防我有超过 10 或 12 个单选按钮
标签: angular typescript ionic2 alert ionic3