[英]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.