简体   繁体   English

如何在角度2中动态获取单选按钮值?

[英]How to fetch radio button value dynamically in angular 2?

Need to display radio button values dynamically from array, and fetch the value of it based on which I need to display a <div> . 需要从数组动态显示单选按钮值,并根据需要显示它的值显示<div> please find the code below 请在下面找到代码

html HTML

<div *ngFor = "let fruit of fruits"
<input type = "radio"/>{{fruit}}>
</div>

component.ts -> contains the following array component.ts->包含以下数组

fruits : string[] = ["apple", "mango"];

With this I am getting the radio buttons apple and mango.Need to get the value of the selected radio button (in assigning[(ngModel)], value) and Need to display another div based on individual selections. 有了这个,我得到了单选按钮apple和mango。需要获取所选单选按钮的值(在赋值[(ngModel)]中,值),并且需要根据个人选择显示另一个div。 Please guide me on this 请指导我

You can use [value] 您可以使用[value]

<div *ngFor="let fruit of fruits">
    <input type="radio" formControlName="options" [value]="fruit">
    {{fruit}}
</div>`
<div *ngFor="let fruit of fruits">
<input type="radio" [value]="fruit" 
(change)="selectedFruitValue(fruit)">
{{fruit}}
</div>

in your ts file
first define a variable
i.e
fruitValue:string

and then write a function 然后写一个函数

selectedFruitValue(fruit){

this.fruitValue = fruit;

//for checking purpose
 console.log('fruit value is':+this.fruitValue);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular:如何以角度动态地实现自定义单选按钮 - Angular:how to implement custom radio button dynamically in angular Angular 7 - 如何将值绑定到单选按钮 - Angular 7 - How to bind the value to the radio button 如何在Angular 4中为单选按钮分配值 - How to assign value to radio button in Angular 4 如何获得这个角度 - 单选按钮的价值? - How to get this angular - radio button's value? 如何使用 Angular Reactive Forms 动态检查单选按钮? - How to dynamically check a radio button with Angular Reactive Forms? 如何在 Angular 中动态检查单选按钮? - How do I check a radio button dynamically in Angular? 从 API 获取值后,如何在 ionic 4 中动态更改多个单选按钮组中的单选按钮值? - How to change radio button value in the multiple radio button group dynamically in ionic 4 after getting the value from API? Angular表格:如何动态获取单选按钮的值? - Angular form: how can I dynamically get the value of radio buttons? 阅读单选按钮值 - Angular 2 - Reading radio button value - Angular 2 如何使用Reactive表单动态设置单选按钮文本? 并选择*单选按钮值和问题标题* onSubmit - How to set radio button text dynamically using Reactive form? and and selected *radio button value and question title * onSubmit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM