[英]Submit all checked radio buttons
我試圖在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
})
我是否應該對貓鼬模型進行任何更改,因為提交后我必須將用戶答案與正確答案進行比較。我覺得用戶答案和正確答案字段應該是一個數組,以便可以將所有選中的問題選項一一存儲。另一方面,對於所有問題,我將如何一次提交所有測試數據。我的angularjs控制器函數邏輯應該是什么樣的。
實際上,您可以針對角度中的所有測試問題使用一系列對象。 完成每個問題后,繼續將對象推入該數組。
[
{
questionId: 123,
userAnswer: 1,
...
},
{
questionId: 123,
userAnswer: 1,
...
},
];
最后,測試完成后,將其提交給API。 另一方面,保持架構的結構化。 不要保留電子郵件之類的冗余數據。 您可以如下簡化。
answerSchema = {
userInfo: {
name: 'abc',
email: 'abc@xyz.com',
attemptedOn: ...,
...
},
testMetaData: {
testId: 1,
testName: 'ABC Test',
...
},
attemptedAnswers: [{
questionId: 1,
attemptedAnswer: 2
},
...
]
};
最好不要包括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.