2017-08-28 76 views
1

我正在使用帶有Ionic 3的JavaScript並在iOS上工作。我正在嘗試實現一個警報與無線電選項供用戶選擇。Ionic 2/3如何訪問無線電提示中的值

我無法使用/訪問選定電臺值警報在下面的代碼:

let alert = this.alertCtrl.create({ 
     title: 'Specify the reason', 
     inputs: [ 
      { 
      type: 'radio', 
      label: 'label 1', 
      value: '0' 
      }, 
      { 
      type: 'radio', 
      label: 'label 2', 
      value: '1' 
      } 
     ], 
     buttons: [ 
      { 
      text: 'Cancel', 
      role: 'cancel', 
      handler:() => { 
       console.log('Cancel clicked'); 
      } 
      }, 
      { 
      text: 'OK', 
      handler:() => { 
       console.log('OK clicked: '); 
       // I NEED TO GET THE VALUE OF THE SELECTED RADIO BUTTON HERE 
      } 
      } 
     ] 
     }); 
     alert.present(); 

回答

1

您可以訪問OK處理程序中的數據:

inputs: [ 
    { 
     name: "myValue", // add a name property 
     type: 'radio', 
     label: 'label 1', 
     value: '0' 
    }, 
    { 
     name: "myValue", // and here 
     type: 'radio', 
     label: 'label 2', 
     value: '1', 
    } 
], 
buttons: [ 
    { 
     text: 'OK', 
     handler: data => { 
      console.log('OK clicked: '); 
      // access through 'data.[nameproperty]' 
      console.log(data.myValue); 
     } 
    } 
] 
1

你在處理程序中獲取無線電數據。檢查the docs ..您的Ok處理程序將有數據作爲參數。

buttons: [ 
     { 
     text: 'Cancel', 
     role: 'cancel', 
     handler:() => { 
      console.log('Cancel clicked'); 
     } 
     }, 
     { 
     text: 'OK', 
     handler: (radiobuttonData) => { 
      console.log('OK clicked: '+ radiobuttonData);//here 
     } 
     } 
    ]