我正在创建一个小测验,我从json文件中获取问题和答案。在您回答完问题后,您需要点击“下一个问题”按钮。通过这样做,我需要检查答案是否正确(并且还记得因为我想在结尾处显示总计)。
我该怎么做?我真的不知道,我刚刚开始使用角度......
<div ng-repeat="question in questions | filter:ids">
{{question.question}}
<div ng-repeat="answer in question.answers"><input type="radio" name="rAnswer" value="{{answer.answer}}">{{answer.answer}}
</div>
<input type="button" ng-click="next()" value="{{buttonText}}">
lycheeControllers.controller('quizCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('json/questions.json').success(function (data) {
//all questions
$scope.questions = data;
//filter for getting answers / question
$scope.ids = function (question) {
return question.id == number;
}
$scope.buttonText = "Next question";
$scope.next = function () {
if (!(number == (data.length))) {
if(number+1==(data.length)){
$scope.buttonText = "Get results";
}
number++;
}
}
});
}]);
答案 0 :(得分:0)
我只需将ng-model="answer.selected"
添加到ng-repeated input
。现在你可以循环查看answer.selected的位置,这就是你的答案。
另一种方法是,如果您不想弄乱模型,可以在控制器中执行$scope.response = [];
之类的操作,然后在模板中设置ng-model="response[$index]"
。然后$ scope.response将保存所有值,您可以循环查看数据的位置。
作为旁注:您可能还会发现设置$ scope.question比使用过滤技术更容易。