0

最初将值添加到表单时,gradingKey.currentAnswer 未绑定到单选按钮。

为什么它不起作用?

这在 angular 4 之前工作过一次,请参阅:How to set the selected radio button最初 in *ngFor on radiobutton group

HTML

<form [formGroup]="gradingKeyForm">

<div class="form-group row">
<label class="col-6 col-form-label">{{getHalfScoresErrorsCount()}}</label>
<div class="col-6">
<span *ngFor="let answer of gradingKey.halfScoresCountAnswers">
    <label class="radio-inline">
        <input type="radio" (ngModelChange)="onChangeHalfScoresErrorsCount($event)"
        formControlName="halfScoresCount" [value]="answer">
        {{answer.value}}
     </label>
</span>
</div>
</div>
</form>

零件

 ngOnInit() {

    this.gradingKeyForm = this.fb.group({       
      halfScoresCount:   this.gradingKey.currentAnswer,
     });
}

模型

import KeyValue from '../keyvalue';
import { IGradingKey } from './shared/grading-key-interface';

export default class GradingKey implements IGradingKey {

  halfScoresCountAnswers: KeyValue<boolean, string>[];
  currentAnswer: KeyValue<boolean, string>;

  constructor(obj: any) {
    this.halfScoresCountAnswers = [new KeyValue(true, 'Ja'), new KeyValue(false, 'Nein')];
    this.currentAnswer = obj.halfScoresCount == true ? this.halfScoresCountAnswers[0] : this.halfScoresCountAnswers[1];
   }
}
4

1 回答 1

1

您可以将 [checked] 属性添加到收音机组,该属性可以作为评估是否应检查收音机的语句。只要单击设置值,问题是无线电组没有反映已更改的数据,它可能对您有用。

相关答案:https ://stackoverflow.com/a/39407224/3934988

因此,假设您的代码在其他任何地方都可以正常工作,那么这样的事情应该可以工作:

  <input type="radio"      (ngModelChange)="onChangeHalfScoresErrorsCount($event)"
    formControlName="halfScoresCount" [value]="answer" [checked]="answer === currentAnswer">
于 2017-07-20T20:21:32.287 回答