【问题标题】:How can i get the selected value in ion-alert radio alert如何在离子警报无线电警报中获得选定的值
【发布时间】:2019-10-01 09:40:43
【问题描述】:
async presentAlertRadio(heading:string){
    const alert = await this.alertCtrl.create({
      header: heading,
      inputs :[
        {
          name : 'Radio 1',
          type: 'radio',
          label: 'Radio 1',
          value: 'value1',
          checked: true
        },
        {
          name: 'radio2',
          type: 'radio',
          label: 'Radio 2',
          value: 'value2'
        },
      ],
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            console.log('Confirm Cancel');
          }
        }, {
          text: 'Ok',
          handler: (data) => {
            console.log('Confirm Ok', data);
          }
        }
      ]
    });
    await alert.present();
  }

我想将我的警报控制器中选定无线电的值分配给一个变量,我如何访问它。

ionic 4 新手,所以不太了解

我想分配我的 var x = selectedRadio

【问题讨论】:

    标签: angular typescript ionic-framework ionic4


    【解决方案1】:

    首先,您必须创建一个变量来存储值。在我的示例中,它被称为selectedAnimal

    async options() {
        const alert = await this.alertController.create({
          cssClass: 'alertOptions',
          header: 'Choose an animal',
          inputs: [
            {
              name: 'cat',
              cssClass: 'ion-radio',
              type: 'radio',
              label: 'Cat',
              value: 'Cat',
              handler: () => {
                
              },
              checked: false
            },
            {
              name: 'dog',
              cssClass: 'ion-radio',
              type: 'radio',
              label: 'Dog',
              value: 'Dog',
              handler: () => { 
              }
            },
          ],
          buttons: [
            {
              text: 'Ok',
              cssClass: 'alert-confirm',
              handler: (data) => {
                console.log('Chosen animal:', data)
                this.selectedAnimal = data;
                if (this.selectedAnimal == undefined) {
                  this.toastSelectionError();
                }
                else{
                  this.toastConfirm();
                }
                
              }
            }
          ]
        });
        await alert.present();
    }
    

    在“确定”按钮中,我将无线电的值处理为data,然后将data 的值赋予selectedAnimal。此外,我添加了一个条件来显示选择是否未定义或是否具有值。

    【讨论】:

      【解决方案2】:
      text: 'Ok',
                    handler: (data) => {
                      console.log('Confirm Ok', data); 
                      this.x = data; 
      }
      

      【讨论】:

      • 您介意用代码解释更新您的答案吗?为什么它可以解决原始问题?这将帮助未来的读者了解正在发生的事情,并了解它为什么起作用,而不仅仅是如何
      【解决方案3】:
            text: 'Ok',
                    handler: (data) => {
                      console.log('Confirm Ok', data); //handle data here eg.
                      this.x = data.value1; // this assigns your veritable 'x' to the value 
                                            // of Radio 1
      }
      

      如果需要澄清,请发表评论

      【讨论】:

        猜你喜欢
        • 2018-05-23
        • 2022-07-08
        • 1970-01-01
        • 2018-08-11
        • 2018-05-16
        • 2021-10-20
        • 1970-01-01
        • 2021-11-11
        • 2019-11-30
        相关资源
        最近更新 更多