2017-07-20 4 views
0

gradingKey.currentAnswerは、最初に値がフォームに追加されたときにラジオボタンにバインドされません。複雑なオブジェクト上のラジオボタンバインディングを使用する角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, 
    }); 
} 

モデル

これは、角度4を参照してくださいする前に一度働いていました

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]; 
    } 
} 
+0

私は 'にチェックを使用することはありませんので、あなたが、plunkerを作成することができます'ここでは、やってみようとするプランクを持っていると助けになるだろう。現在、問題を再現するのに十分なコードがありません。 – Alex

+0

だから...あなたはすでに上記のコードで "バグ"を再現しようとしましたか?実際には必要なものはすべてそこにあります。後でplunkrを作成することができます... – Pascal

答えて

0

ラジオをチェックするかどうかを評価するためのステートメントとなるラジオグループに[checked]プロパティを追加できます。クリックが値を設定している間、変更されたデータを反映していないラジオグループが問題である場合、おそらくあなたのために働くでしょう。

関連の答え:https://stackoverflow.com/a/39407224/3934988

だからあなたのコードは、それが他のどこでも必要として動作しますが、このようなものが動作するはずと仮定:

<input type="radio"  (ngModelChange)="onChangeHalfScoresErrorsCount($event)" 
    formControlName="halfScoresCount" [value]="answer" [checked]="answer === currentAnswer"> 
+0

Huh?... – JGFMK

+0

反応したフォームでチェック属性を実際に使用する必要がありますか? – Pascal

+0

ちょうどチェック...それは、ありがとう、ありがとう。 – Pascal

関連する問題