【问题标题】:How to set the value of radio button alert in Ionic 2如何在 Ionic 2 中设置单选按钮警报的值
【发布时间】: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


【解决方案1】:

是的,有一个简单的方法可以做到这一点。您可以像这样在checked 属性中添加条件:

selectColor(color){
    let alert1 = this.alertCtrl.create();
    alert1.setTitle('Color Theme');

    alert1.addInput({
        type: 'radio',
        label: 'Red',
        value: 'red',
        checked: color === "red"
    });

    alert1.addInput({
        type: 'radio',
        label: 'Pink',
        value: 'pink',
        checked: color === "pink"
    });

    alert1.addInput({
        type: 'radio',
        label: 'Purple',
        value: 'purple',
        checked: color === "purple"
    });

    alert1.addInput({
        type: 'radio',
        label: 'Blue',
        value: 'blue',
        checked: color === "blue"
    });

    // ...

    alert1.addButton('Cancel');
    alert1.addButton({
        text: 'Okay',
        handler: data => {
            // ...
        }
    });

    alert1.present();
}

编辑

还有一种添加所有颜色的更好方法,即使用 forEach 循环和包含所有颜色的数组:

selectColor(color){
    let alert1 = this.alertCtrl.create();
    alert1.setTitle('Color Theme');

    // Add the new colors here!
    const colors = ['Red', 'Pink', 'Purple', 'Blue'];

    colors.forEach(color => {
        alert1.addInput({
            type: 'radio',
            label: color,
            value: color.toLowerCase(),
            checked: color === color.toLowerCase()
        });
    });

    // ...

    alert1.addButton('Cancel');
    alert1.addButton({
        text: 'Okay',
        handler: data => {
            // ...
        }
    });

    alert1.present();
}

【讨论】:

  • 感谢您的回答。我需要重复第一个代码 5 到 7 次,但你用第二个代码拯救了我的心。再次感谢你
  • 很高兴听到这个消息:)
  • 如何在警报中设置日期时间输入字段?
猜你喜欢
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多