1

我有点卡在这里,我是 angular js 的新手。

我有一个包含许多问题的表格。这些问题正在加载 angularjs。

表单的 html 是

<form ng-controller="controller" ng-submit="submitFirstQuiz()">
<div ng-repeat = "question in quiz.questions">
{{question}}
<label data-ng-repeat="choice in quiz.choices" style="float:left;">
    <input name="{{question}}" type="radio" value="{{choice}}"/>
</label>
<br/>
</div>
<input type="submit" value="submit"></input>
</ng-form>

我的js是

'use strict';

var QuizModule = angular.module('quiz');

QuizModule.controller('PersonalityQuizCtrl', function($scope){

    $scope.quiz = {
            questions: ["q1","q2","q3"],
            choices: ["5","4","3","2","1"]
        };

    $scope.submitFirstQuiz = function(){
        console.debug("how to print selected choices");
    };

});

上面的代码正在生成我想要的表单。现在我如何获取单选按钮的选定值,换句话说,我如何提交具有选定值的表单。我不知道如何绑定结果数据。请帮助我,我将不胜感激。问候,

4

1 回答 1

1

HTML

<input name="{{question}}" ng-click="radioSelected(choice, question)" 
type="radio" value="{{choice}}"/>

JS

'use strict';

var QuizModule = angular.module('quiz');

QuizModule.controller('PersonalityQuizCtrl', function($scope){

    $scope.quiz = {
            questions: ["q1","q2","q3"],
            choices: ["5","4","3","2","1"]
        };

    $scope.selectedChoices = {};

    $scope.radioSelected = function(choice, question){        
        $scope.selectedChoices[question] = choice;
    }
    $scope.submitFirstQuiz = function(){
        console.log($scope.selectedChoices);
    };

});
于 2013-08-12T12:05:22.657 回答