将复选框值绑定到angularjs中的相应数组

时间:2013-08-16 11:56:58

标签: angularjs angularjs-scope angularjs-ng-repeat

我在运行时使用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:

        }

1 个答案:

答案 0 :(得分:1)

如何像这样重写addChoice函数

$scope.addChoice = function (question, choice, questionType) {
    userInterestAnswers[question].answer.push(choice);
}

如果您可以在foreach问题中添加类型字段,则可以使用ng-switch显示复选框或单选按钮,以便合并2个中继器。尝试重写这种模式

<div ng-repeat ... >
    <div ng-switch on "question.type">
        <input ng-switch-when="multichoice" type="checkbox" ... >
        <input ng-switch-when="singlechoice" type="radio" ... >
    </div>
</div>