简体   繁体   English

提交所有选中的单选按钮

[英]Submit all checked radio buttons

I'm trying to make a quiz app with the help of MEAN stack.After all the questions are attempted there will be submit button to submit all the checked radio buttons.Like for option1 it should be 1,for option2 it should be 2.Currently my answer mongoose model looks like this- 我试图在MEAN stack的帮助下制作一个测验应用程序。尝试所有问题后,将有一个提交按钮来提交所有选中的单选按钮。像option1一样应该是1,for option2应该是2。目前,我的答案猫鼬模型看起来像这样-

const answerSchema = new Schema({

                              userEmail: {
                                type: String, require:true
                              },
                              testId: {
                                type: String, require:true
                              },
                              questionId: {
                                type: String, require:true
                              },
                              userAnswer: {
                                type: String
                              },
                              correctAnswer: {
                                type: String, require:true
                              },
                              timeTakenEach: {
                                type: Number,
                                default: 1
                              }  //insecs

})

Should I make any changes to the mongoose model because after submission I have to compare the useranswer with the correct answer.I feel like the useranswer and correctanswer fields should be an array so that all the questions checked option can be stored one by one.On the other hand how will I submit all the test data at once for all questions.What should my angularjs controller function logic be like. 我是否应该对猫鼬模型进行任何更改,因为提交后我必须将用户答案与正确答案进行比较。我觉得用户答案和正确答案字段应该是一个数组,以便可以将所有选中的问题选项一一存储。另一方面,对于所有问题,我将如何一次提交所有测试数据。我的angularjs控制器函数逻辑应该是什么样的。

You can actually have an array of objects for all the test questions in angular. 实际上,您可以针对角度中的所有测试问题使用一系列对象。 After the completion of every question, keep pushing object to this array. 完成每个问题后,继续将对象推入该数组。

[
  {
    questionId: 123,
    userAnswer: 1,
    ...
  },
  {
    questionId: 123,
    userAnswer: 1,
    ...
  },
];

Finally when the test is complete, submit it to the API. 最后,测试完成后,将其提交给API。 On the other hand, keep your schema structured. 另一方面,保持架构的结构化。 Do not keep redundant data like email. 不要保留电子邮件之类的冗余数据。 You can simplify this as follows. 您可以如下简化。

answerSchema = {
  userInfo: {
    name: 'abc',
    email: 'abc@xyz.com',
    attemptedOn: ...,
    ...
  },
  testMetaData: {
    testId: 1,
    testName: 'ABC Test',
    ...
  },
  attemptedAnswers: [{
    questionId: 1,
    attemptedAnswer: 2
  },
  ...
  ]
};

Better don't include correctAnswer in the answerSchema instead have it in the separate collection because it will extra space in the DB for each user 最好不要包括correctAnswer在answerSchema而是有它的单独收集,因为在数据库中为每个用户它将额外的空间

const answerSchema = new Schema({

    userEmail: {
        type: String, require: true
    },
    testId: {
        type: String, require: true
    },
    questionId: {
        type: String, require: true
    },
    userAnswer: {
        type: String
    },
    timeTakenEach: {
        type: Number,
        default: 1
    }  //insecs

});

const questionAnswer = new Schema({
    questionId: {
        type: String, require: true
    },
    correctAnswer: {
        type: String, require: true
    },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM