2017-06-05 77 views
1

需要從數組中動態顯示單選按鈕值,並獲取其值,我需要顯示一個<div>。請在下面找到如何以角度2動態獲取單選按鈕值?

HTML

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

component.ts代碼 - >包含以下陣列

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

有了這個我收到單選按鈕蘋果和mango.Need來獲取值(分配[(ngModel)],值),並且需要根據單個選擇顯示另一個div。 請指導我在此

回答

2

您可以使用[value]

<div *ngFor="let fruit of fruits"> 
    <input type="radio" formControlName="options" [value]="fruit"> 
    {{fruit}} 
</div>` 
0
<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 

,然後寫一個函數

selectedFruitValue(fruit){ 

this.fruitValue = fruit; 

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

}