无法在单选按钮上使用ngForm绑定数据

时间:2019-12-02 10:18:59

标签: angular

有人可以帮忙吗,我正在尝试将数据绑定到单选按钮,但似乎没有任何内容输出到问卷调查表值。!

<form #questionnaire="ngForm">
   {{questionnaire.value | json}}    
   <div>
      <label>Float label: </label>
      <mat-radio-group ngModel name="answer1" formControlName="floatLabel">
         <mat-radio-button name="answer1" ngModel value="this">This</mat-radio-button>
         <mat-radio-button name="answer1" ngModel value="that">That</mat-radio-button>
         <mat-radio-button name="answer1" ngModel value="other">Other</mat-radio-button>
      </mat-radio-group>
   </div>
</form>

2 个答案:

答案 0 :(得分:1)

尝试这样:

<mat-radio-group [(ngModel)]="answer1" #radioGroup="matRadioGroup">
  <mat-radio-button name="answer1" value="this">this</mat-radio-button>
  <mat-radio-button name="answer1" value="that">That</mat-radio-button>
  <mat-radio-button name="answer1" value="other">Other</mat-radio-button>
</mat-radio-group>

Working Demo

答案 1 :(得分:0)

按如下所示修改代码,

<form #questionnaire="ngForm">
   {{ selectedRadioValue }}  
   <div>
      <label>Float label: </label>
      <mat-radio-group [(ngModel)]="selectedRadioValue" name="answer1" formControlName="floatLabel">
      <mat-radio-button name="answer1" value="this">This</mat-radio-button>
      <mat-radio-button name="answer1" value="that">That</mat-radio-button>
      <mat-radio-button name="answer1" value="other">Other</mat-radio-button>
      </mat-radio-group>
   </div>
</form>