Javascript AngularJS:检查单选按钮并在末尾添加一个新的分部
我已经使用AngularJS有一段时间了。我有一个单选按钮,它为用户提供选项和具有正确答案值的json数据。我想检查用户提供的正确答案的数量(在用户单击“提交”按钮后),然后在测试结束时显示。我如何做到这一点 COntrollers.jsJavascript AngularJS:检查单选按钮并在末尾添加一个新的分部,javascript,angularjs,json,Javascript,Angularjs,Json,我已经使用AngularJS有一段时间了。我有一个单选按钮,它为用户提供选项和具有正确答案值的json数据。我想检查用户提供的正确答案的数量(在用户单击“提交”按钮后),然后在测试结束时显示。我如何做到这一点 COntrollers.js var testControllers = angular.module('testControllers', []); testControllers.controller('AnsaController', ['$scope', function($
var testControllers = angular.module('testControllers', []);
testControllers.controller('AnsaController', ['$scope', function($scope) {
$scope.check = function check(){
alert("He");
};
}]);
testControllers.controller('ListController', ['$scope', '$http', function($scope, $http) {
$http.get('js/data.json').success(function(data) {
$scope.questions = data;
$scope.artistOrder = 'name';
});
}]);
testControllers.controller('DetailsController', ['$scope', '$http','$routeParams' ,function($scope, $http, $routeParams) {
$http.get('js/data.json').success(function(data) {
$scope.questions = data;
$scope.whichItem = $routeParams.itemId;
$scope.parseInt = parseInt;
if($routeParams.itemId > 0){
$scope.prevItem = Number($routeParams.itemId) - 1;
}
else{
$scope.prevItem = $scope.questions.length - 1;
}
if($routeParams.itemId < $scope.questions.length-1){
$scope.nextItem = Number($routeParams.itemId) + 1;
}
else{
$scope.nextItem = 0;
}
});
}]);
HTML:
问题{parseInt(whichItem)+1}
-
您的答案已被记录
您已经选择:{{selopt}
您可以将用户的答案存储在会话存储中,并在需要时检索。请注意,sessionStorage仅在当前选项卡上存储数据,当您关闭该选项卡时,数据将被删除。如果您想要多选项卡会话存储,请参阅单击“提交”按钮后如何比较?单击“提交”按钮。。ng单击=“submitFunction()”并处理角度控制器中的逻辑。例如:$scope.submitFunction()=function(){$scope.tab=1;//添加逻辑以将答案保存在会话存储中}它不起作用。我会更新我的问题,如果你点击两次。移除第一个并保留ng click=“check()”。现在,在check函数中添加$scope.tab=1和将答案保存到sessionStorage的逻辑
[{
"pos": "A",
"val": "3"
}, {
"pos": "B",
"val": "6"
}, {
"pos": "C",
"val": "9"
}, {
"pos": "D",
"val": "0"
}, {
"answer": "B"
}]
<link rel="stylesheet" href="css/common.css" />
<section class="artistinfo" ng-controller = "AnsaController">
<div class="mcq" ng-model = "questions">
<center><a href = "#details/{{prevItem}}" class = "button">PREV</a>
<a href = "#details/{{nextItem}}" class = "button">NEXT</a></center>
<div class = "qanda">
<h3 class = "qnum">QUESTION {{parseInt(whichItem)+1}}</h3>
<p class = "ques" math-jax-bind = "questions[whichItem].ques"></p>
<ul ng-show = "tab!= 1 || selopt==null">
<li class= "optlist" ng-repeat="item in questions[whichItem].opts">
<label class="formgroup">
<input type="radio" name = "q" ng-model = "$parent.selopt" value = "{{item.pos}}"><span math-jax-bind = "item.val"></span>
</label>
</li>
<center><button class = "button" ng-click = "tab = 1" ng-click = "check();"></button></center>
</ul>
<div ng-show = "selopt !=null">
<p class = "ques" ng-show = "tab === 1">Your answer has been recorded! </p>
</div>
<p class = "ques">You've chosen: {{selopt}}</p>
</div>
<center><a href = "#list" class = "button">List View</a></center>
</div>
</section>