I can't get the values for radio buttons in An

2019-09-20 06:01发布

问题:

I can't get the values for radio button inputs in angular 2 forms to show up.

回答1:

I was able to get the values using RadioButtonState.

https://angular.io/docs/ts/latest/api/common/index/RadioButtonState-class.html

Template

<div>
      <h1>form test</h1>
      {{isAdminYes.checked}}
      <form [ngFormModel]="regForm">
        <input type="radio" ngControl="isAdmin" name="isAdmin" [checked]="true" [(ngModel)]="isAdminYes" > Yes 
        <input type="radio" ngControl="isAdmin" name="isAdmin" [(ngModel)]="isAdminNo" > No
      </form>
    </div>

Component

export class App {
  public isAdminYes : RadioButtonState = new RadioButtonState(true, "yes");
  public isAdminNo : RadioButtonState = new RadioButtonState(false, "no");

  constructor() {
    this.regForm = new ControlGroup({
        isAdmin: new Control(true)
    });
  }
}

Working plunker http://plnkr.co/edit/NnT8uRAr3xjxKB1hYfxT?p=preview