【发布时间】:2017-12-26 17:10:51
【问题描述】:
最初将值添加到表单时,gradingKey.currentAnswer 未绑定到单选按钮。
为什么它不起作用?
这在 angular 4 之前工作过一次,请参阅:How to set the selected radio button initially 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];
}
}
【问题讨论】:
-
你能创建一个 plunker,因为我不会在这里使用
checked,并且有一个 plunker 可以帮助。目前没有足够的代码来重现该问题。 -
那么...您是否已经尝试使用上述代码重现“错误”?实际上一切都在那里需要什么。我可以稍后创建一个 plunkr...
标签: angular angular-forms