Ionic 如何在使用Angular 提交后获取所选离子单选按钮的值

30byixjq  于 2022-12-09  发布在  Ionic
关注(0)|答案(1)|浏览(177)

.html格式

<ion-radio-group 
    *ngFor="let option of data" 
    (ionChange)="checkValue($event)" 
    [value]="item">
   <ion-item>
       <ion-label>{{option.optionA}}</ion-label>
       <ion-radio slot="start" value="Not"></ion-radio>
   </ion-item>   
   <ion-item>
       <ion-label>{{option.optionB}}</ion-label>
       <ion-radio slot="start"  value="Some"></ion-radio>
   </ion-item>
   <ion-item>
       <ion-label>{{option.optionC}}</ion-label>
       <ion-radio slot="start" value="Most"></ion-radio>
   </ion-item>
   <ion-item>
       <ion-label>{{option.optionD}}</ion-label>
       <ion-radio slot="start" value="All"></ion-radio>
   </ion-item>
</ion-radio-group>

<div class="evalbtn">
    <button (click)="print(event)" class="nextbtn" (click)="evaluation2()">
        Submit
    </button>
</div>

文件

data: any[] = [
  {
    "optionA":"Not at All",
    "optionB":"For some of the Time",
    "optionC":"For most of the Time",
    "optionD":"For all of the Time",      
  }
];

//function to print what is inputed in the form that is declared above
checkValue(event) { 
    console.log(event.detail.value)
}
 
print(event) {
    console.log(this.checkValue(event))
}
a64a0gku

a64a0gku1#

对于ion-radio-group,添加[(ngModel) ]而不是[value]

<ion-radio-group *ngFor="let option of data"
    [(ngModel)]="selectedValue"
    (ionChange)="checkValue($event)">
<!-- items -->
</ion-radio-group>

并将selectedValue添加到组件中:

selectedValue: any;

print(event) {
    console.log('Selected value: ', this.selectedValue);
}

相关问题