Angular 2:点击div上的单选按钮

时间:2017-07-03 20:49:29

标签: angular angular2-forms

我想检查单选按钮,获取值并在用户点击div时将section'selected'添加到sectionBlock

请帮助你不要在角度2

中做到这一点

selectPlant(){
    console.log('');
  }
<div class="SectionContent">
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="AEP" /><span>AEP</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="ELP" /><span>ELP</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="HCL" /><span>HCL</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="HCM" /><span>HCM</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="HDA" /><span>HDA</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="HMA" /><span>HMA</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="HMI" /><span>HMI</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="HSC" /><span>HSC</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="MAP" /><span>MAP</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="MMP" /><span>MMP</span></div>
    <div class="sectionBlock" (click)="selectPlant($event)"><input type="radio" class="plant" name="plant" value="PMC" /><span>PMC</span></div>
  </div>

1 个答案:

答案 0 :(得分:0)

当选择收音机时使用指令ngClass,你有一些变量selectedInput

一个例子

div [ngClass]="{'sectionBlock': selectedInput}" (click)="selectPlant('AEP')">