【发布时间】:2014-01-12 19:52:20
【问题描述】:
我正在创建一个小测验,我从一个 json 文件中获取我的问题和答案。回答完一个问题后,您需要单击“下一个问题”按钮。通过这样做,我需要检查答案是否正确(还要记住,因为我想在最后显示总分)。
我该怎么做?我真的不知道,我刚开始使用角度......
HTML
<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++;
}
}
});
}]);
【问题讨论】:
标签: angularjs controller radio-button