2

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.