I'm having trouble understanding why a "simple" ngModel form is not functioning as the resources suggests it should.
The form holds a quiz with a series of multichoice (radio button) questions. When the quiz is generated, the form elements in my template are correctly populated e.g. the value attribute for each question option is entered as are some hidden fields associated with each question. But when submitted, none of the selected options were 'recorded' - just the element names were submitted.
I then read that each element need to have ngModel added to it. But when I did that, the value attributes were not populated at all.
My template is:
<form id="quizForm" #f="ngForm" (ngSubmit)="onSubmit(f)" class="form-horizontal">
<div *ngFor="let item of aData; let i=index">
<div class="qunit">
<p class="question"><strong>Question {{i+1}}.</strong> <span [innerHTML]="item['question'] | keepHtml"></span></p>
<ul class="phoriz nobullet">
<li class='mcqoptions' *ngFor="let mcq of item.mcq">
<label>
<input ngModel type="{{item['qtype']}}" class="rightpad" name="option{{i}}" [value]="mcq['mcqID']"><span [innerHTML]="mcq['mcqoption'] | keepHtml"></span>
</label>
</li>
</ul>
<p id="qfb{{item['questionID']}}" class="quizfback"></p>
<input ngModel type="hidden" name="noptions{{i}}" value="4" />
<input ngModel type="hidden" name="questionID{{i}}" value="{{item['questionID']}}"/>
<input ngModel type="hidden" name="qType{{i}}" value="{{item['quizquestiontypeID']}}" />
</div>
</div>
<div style="padding:20px 0" class="form-group">
<div class="col-sm-12 controls">
<button type="submit" class="btn btn-success">Submit this quiz</button>
</div>
</div>
</form>
Submitting the form triggers this function:
onSubmit(f):void{
console.log(f);
}
I am importing 'FormsModule' and 'NgForm' into the component.
I guess the first question is why is the presence of 'ngModel' in each radio input and hidden input element negatively impacting on those elements. The second question is how do I get the values to be submitted (but perhaps the need for that question will disappear once the first one is dealt with).
Thanks/Tom