Angular2 - RadioButtonState无法正常工作

时间:2016-05-10 14:50:23

标签: javascript angular radio

我在尝试使RadioButtonState正常工作时遇到了一些麻烦。我尝试过以下方式使用RadioButtonState:

import {Component} from '@angular/core';
import {RadioButtonState} from '@angular/common';
import {bootstrap} from '@angular/platform-browser-dynamic';

@Component({
    selector: 'my-app',
    template:`
    Select Gender:
    <span *ngFor="let gender of inputs; let i=index" >
        <input type="radio" name="gender" [(ngModel)]="inputs[i]" /> {{gender.value}}
    </span>

    <br>
    {{inputs | json}}
    `
})
export class AppCmp{
    public inputs = [
        new RadioButtonState(false,"Male"),
        new RadioButtonState(false,"Female")
    ]
}

bootstrap(AppCmp);

当我单击任何单选按钮时,它将值设置为true,但在单选按钮之间切换不会再将其中任何一个设置为false。我重新解决了问题here on plunker

1 个答案:

答案 0 :(得分:2)

这是一个已知问题https://github.com/angular/angular/issues/7374

提到的解决方法,直到解决此问题:

<label class="radio-inline">
  <input type="radio"
      value="male" 
      #male
      [checked]="child.gender === 'male'"
      (click)="child.gender = male.value"
      name="gender">Male</label>                   

<label class="radio-inline">
  <input type="radio"
      #female
      value="female"
      [checked]="child.gender === 'female'" 
      (click)="child.gender = female.value" 
      name="gender">Female</label>