我正在AngularJS中构建测验应用程序,但是我不知道如何遍历问题以添加和显示正确的答案。
请帮助我是编程新手。
JSON格式的测验如下所示:
"questions": [{
"Id": 5,
"Name": "In a village, the number of people infected with HIV in 2001 was 100. In 2004, 250 people were infected. Find the ratio of those infected in 2001 to those infected in 2004.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. 1:5", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. 2:5", "Answer": true },
{ "Id": 3, "QuestionId": 1, "Name": "C. 3:5", "Answer": false },
{ "Id": 4, "QuestionId": 1, "Name": "D. 5:1", "Answer": false },
{ "Id": 5, "QuestionId": 1, "Name": "E. 5:2", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
},
{
"Id": 6,
"Name": "The ratio of the length to the breadth of a room is 7:5. Find the length of the room if the breadth is 10m.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. 10m", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. 12m", "Answer": false },
{ "Id": 3, "QuestionId": 1, "Name": "C. 14m", "Answer": true },
{ "Id": 4, "QuestionId": 1, "Name": "D. 21m", "Answer": false },
{ "Id": 5, "QuestionId": 1, "Name": "E. 35m", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
},
{
"Id": 7,
"Name": "Increase 80 by 10%.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. 8m", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. 10m", "Answer": false },
{ "Id": 3, "QuestionId": 1, "Name": "C. 80m", "Answer": false },
{ "Id": 4, "QuestionId": 1, "Name": "D. 88m", "Answer": true },
{ "Id": 5, "QuestionId": 1, "Name": "E. 108m", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
},
{
"Id": 8,
"Name": "Odiri bought 12 cartons of biscuits for N3,000.00. Find the cost of 7.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. ₦250.00", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. ₦442.00", "Answer": false },
{ "Id": 3, "QuestionId": 1, "Name": "C. ₦840.00", "Answer": false },
{ "Id": 4, "QuestionId": 1, "Name": "D. ₦1,750.00", "Answer": true },
{ "Id": 5, "QuestionId": 1, "Name": "E. ₦2,700.00", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
},
答案 0 :(得分:0)
您想使用const promises = [];
worksheet.eachRow(function(row, rowNumber) {
console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
const promise = db.insert(row); // <-- whatever async operation you have here
promises.push(promise);
});
Promise.all(promises).then((result)=>{
console.log("Done")
}).catch((err) => {
console.log("An error occurred while inserting data", err);
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
一旦进入对象,您就可以使用JSON.parse()
答案 1 :(得分:0)
这是poc: check answers thanks to radio buttons and show correct/false thanks to ng-if 您必须检查文档/执行教程才能提高技能 也许下次例如在像stackblitz这样的平台上发送第一个作品。
答案 2 :(得分:0)
我对它有裂痕,请查看密码笔
https://codepen.io/thefallen78/pen/ZPOgKL
(function () {
'use strict';
angular.module("myapp",[]).controller("helloController", function($scope){
$scope.quiz = {"questions": [{
"Id": 5,
"Name": "In a village, the number of people infected with HIV in 2001 was 100. In 2004, 250 people were infected. Find the ratio of those infected in 2001 to those infected in 2004.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. 1:5", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. 2:5", "Answer": true },
{ "Id": 3, "QuestionId": 1, "Name": "C. 3:5", "Answer": false },
{ "Id": 4, "QuestionId": 1, "Name": "D. 5:1", "Answer": false },
{ "Id": 5, "QuestionId": 1, "Name": "E. 5:2", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
},
{
"Id": 6,
"Name": "The ratio of the length to the breadth of a room is 7:5. Find the length of the room if the breadth is 10m.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. 10m", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. 12m", "Answer": false },
{ "Id": 3, "QuestionId": 1, "Name": "C. 14m", "Answer": true },
{ "Id": 4, "QuestionId": 1, "Name": "D. 21m", "Answer": false },
{ "Id": 5, "QuestionId": 1, "Name": "E. 35m", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
},
{
"Id": 7,
"Name": "Increase 80 by 10%.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. 8m", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. 10m", "Answer": false },
{ "Id": 3, "QuestionId": 1, "Name": "C. 80m", "Answer": false },
{ "Id": 4, "QuestionId": 1, "Name": "D. 88m", "Answer": true },
{ "Id": 5, "QuestionId": 1, "Name": "E. 108m", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
},
{
"Id": 8,
"Name": "Odiri bought 12 cartons of biscuits for N3,000.00. Find the cost of 7.",
"QuestionTypeId": 1,
"Options": [
{ "Id": 1, "QuestionId": 1, "Name": "A. ₦250.00", "Answer": false },
{ "Id": 2, "QuestionId": 1, "Name": "B. ₦442.00", "Answer": false },
{ "Id": 3, "QuestionId": 1, "Name": "C. ₦840.00", "Answer": false },
{ "Id": 4, "QuestionId": 1, "Name": "D. ₦1,750.00", "Answer": true },
{ "Id": 5, "QuestionId": 1, "Name": "E. ₦2,700.00", "Answer": false }],
"QuestionType": { "Id": 1, "Name": "Multiple Choice", "Active": true }
}]}
});
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>the quiz</title>
</head>
<body ng-app = "myapp">
<div class = "container">
<div class = "row" ng-controller = "helloController">
<div class = "col-md-6 col-md-offset-3" style = "background-color:red;">
<form action = "POST" ng-repeat = "(key,value) in quiz">
<label>{{key}}</label>
<div class = "form-group" ng-repeat = "item in value">
<label>{{item.Name}}</label>
<select class = "form-control" >
<option ng-repeat = "options in item.Options">{{options.Name}}</option>
</select>
</div>
</form>
</div>
</div>
</div>
</body>
</html>