Get values from multiple radio group in angular

87 views Asked by At

I want to get selected radio button value then create an object to send to database, I have working example here https://stackblitz.com/edit/angular-1frcbc .I got what i expected object like this

{
    lessonCode: 'test',
    answers: [
            {
        questionCode : 'question-1',
        optionCode : 'option-2'
        },
            {
        questionCode : 'question-2',
        optionCode : 'option-3'
        },
    ]       
}

but there is an error ERROR like

Error: Error trying to diff 'Option 2'. Only arrays and iterables are allowed

I believe there is something wrong in here

  <input type="radio" id="{{option.order}}" [(ngModel)]="question.options" value="{{option.code}}" name="{{question.order}}">

can someone help me? any thoughts would be helpful

EDIT

i know that question.options can't assign to ngModel if i change it to something like option.order .I'm not getting object like what i expected above, if i change to something like that.

I'm pretty sure i need to change this code

  getJsonResult() {
    let answer: any;
    let data = {
      lessonCode: "test",
      answers: this.data.questions.map(x => {
        return {
          questionCode: x.code,
          optionCode: x.options
        };
      })
    };
    this.submitData = data;
    console.log(JSON.stringify(this.submitData))
  }
1

There are 1 answers

0
naoval On

i accidentally change my code to something like this

    return {
      questionCode: x.code,
      status: x.options.code
    };

and in the html like this

<input type="radio" id="{{option.order}}" [(ngModel)]="question.options.code" value="{{option.code}}" name="{{question.order}}">

and it works and give me the same result, hope this will help someone