最初將值添加到窗體時,gradingKey.currentAnswer未綁定到單選按鈕。在複雜對象上使用單選按鈕綁定的角度4反應形式不起作用
爲什麼不起作用?
此之前角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,因爲我不會使用'檢查'在這裏,並且有一個搗蛋者來修補會有所幫助。目前沒有足夠的代碼來重現問題。 – Alex
所以......你是否已經嘗試用上面的代碼重新編制「bug」?其實一切都在那裏需要什麼。我可以稍後創建一個plunkr ... – Pascal