我有一个使用 angularjs 在运行时创建的表单。此表格有 6 个问题,其中问题 1 的答案是单选按钮,而其余问题的答案是复选框。
表单创建为
<form ng-controller="PersonalityCtrl" ng-submit="submitInterests()">
<div ng-repeat="question in interestQuestions.slice(0,1)" style="display: block;">
<br> <span style="float: left">{{question.question}}</span> <br />
<span ng-repeat="(key, value) in question.choices">
<input name="{{question.number}}"
ng-click="addChoice(question.number, key,question.questionType)"
type="radio" value="{{key}}" required />
{{value}}
</span> <br />
</div>
<div ng-repeat="question in interestQuestions.slice(1,6)" style="display: block;">
<br> <span style="float: left">{{question.question}}</span> <br />
<span ng-repeat="(key, value) in question.choices">
<input name="{{question.number}}"
ng-click="addChoice(question.number, key,question.questionType)"
type="checkbox" value="{{key}}" />
{{value}}
</span> <br />
</div>
<input type="submit" value="submit"></input>
</ng-form>
我生成的表格看起来像
Question : 1
0 0 0 0 0 (0 represents radio button)
Question : 2
o o o o o (o represents check box)
Question : 3
o o o o o (o represents check box)
Question : 4
o o o o o (o represents check box)
当我提交表格时,我的帖子数据应该是表格
[{1:[list of answers]},{2:[list of answers]},{...},{...},{...},{...}]
其中 1,2 代表问题编号,而答案列表是复选框的值。
我的问题是如何使用 angularjs 将每个问题的答案保存在单独的数组中。
目前我正在这样做(但它似乎不是有角度的方式)。
var q1 = {
questionNumber : 1,
answer : new Array()
};
var q2 = {
questionNumber : 2,
answer : new Array()
};
var q3 = {
questionNumber : 3,
answer : new Array()
};
var q4 = {
questionNumber : 4,
answer : new Array()
};
var q5 = {
questionNumber : 5,
answer : new Array()
};
var q6 = {
questionNumber : 6,
answer : new Array()
};
var userInterestAnswers = [ q1, q2, q3, q4, q5, q6 ];
var choiceList = new Array();
$scope.addChoice = function(question, choice, questionType) {
switch (question) {
case 1:
q1.answer.push(choice);
break;
case 2:
q2.answer.push(choice);
break;
case 3:
q3.answer.push(choice);
break;
case 4:
q4.answer.push(choice);
break;
case 5:
q5.answer.push(choice);
break;
case 6:
q6.answer.push(choice);
break;
default:
}