I need a dynamic number of radio buttons based on radio name to create an object for example, I'm expecting an output of something like this
Expected Output
[
{Q1: '1'},
{Q2: '3'}
];
I tried by using [(ngModel)]
But, when I click any radio, always the last one gets selected and the value is never assigned to ngModel.
If I remove ngModel, the radios work fine, but, value is not set and only value can be selected at time. What can be done here?
app.component.html
<div *ngFor="let question of questions">
<ul>
<li>
<span>{{question.id}}.{{question.name}}</span>
<div *ngFor="let radio of radioGroup">
<input id="{{question.radioName}}"
class="radio-button"
[value]='radio.id'
type="radio"
name="radio"/>
<div class="radio-tile">
<label for="{{question.radioName}}"
class="radio-tile-label">
{{radio.name}}
</label>
</div>
</div>
</li>
</ul>
</div>
app.component.ts
export class AppComponent {
questions;
radioGroup;
ngOnInit() {
this.questions =[
{
id: '1',
name: 'What is your name?',
radioName: 'Q1',
modelName: 'question1'
},
{
id: '2',
name: 'What is your role in your organization?',
radioName: 'Q2',
modelName: 'question2'
}
];
this.radioGroup = [
{id: '1', name: 'Strongly Disagree'},
{id: '2', name: 'Disagree'},
{id: '3', name: 'Neutral'},
{id: '4', name: 'Agree'},
{id: '5', name: 'Strongly Agree'},
];
}
}