我正在尝试编写一个测验app.Below是源代码。目前它的工作原理如下:
依旧......
请检查链接http://plnkr.co/edit/sYG9jjX0JATbQhJ1hNf6
我想要实现的目标如下:
请在下面找到代码:
的index.html
<!DOCTYPE html>
<html ng-app="quizApp">
<head>
<meta charset="utf-8" />
<title>QuizApp</title>
<link rel="stylesheet" href="style.css" />
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div class="container">
<h1 class="title">QuizApp</h1>
<quiz/>
</div>
</body>
</html>
template.html
<div class="quiz-area" ng-show="inProgress">
<div ng-show="!quizOver" ng-repeat="option1 in question" ng-init="parentIndex = $index">
<h2 id="question">{{option1.question}}</h2>
<div id="options">
<div ng-model="selected" ng-repeat="option in option1.options">
<label>
<input type="radio" ng-value="option.name" ng-model="option.selectedRadio" name="myName{{$parent.$index}}">
{{option.name}}
</label>
</div>
</div>
<div>
<button ng-click="checkAnswer(option1)" class="next-question">Next</button>
</div>
</div>
<div ng-show="quizOver">
<h2>Quiz is over</h2>
<button ng-click="reset()">Play again</button>
</div>
<div id="score">
Score: {{score}}
</div>
</div>
<div class="intro" ng-show="!inProgress">
<p>Welcome to the QuizApp</p>
<button id="startQuiz" ng-click="start()">Start the Quiz</button>
</div>
app.js
var app = angular.module('quizApp', []);
app.directive('quiz', function(quizFactory) {
return {
restrict: 'AE',
scope: {},
templateUrl: 'template.html',
link: function(scope, elem, attrs) {
scope.start = function() {
scope.id = 0;
scope.score = 0;
scope.question = [];
scope.quizOver = false;
scope.inProgress = true;
scope.getQuestion();
};
scope.reset = function() {
scope.inProgress = false;
scope.score = 0;
}
scope.getQuestion = function() {
var q = quizFactory.getQuestion(scope.id);
console.log(q);
if (q) {
scope.object = {
'question': q.question,
'options': q.options,
'answer': q.answer,
'trueQId': q.trueQId,
'falseQid': q.falseQid,
'answerMode': true,
'answers': q.answers
}
scope.question.push(scope.object);
console.log(scope.question);
} else {
scope.quizOver = true;
}
};
scope.checkAnswer = function(question) {
var options = question.options;
for (var i = 0; i < options.length; i++) {
if (options[i].selectedRadio) {
var ans = options[i].selectedRadio;
}
}
console.log(question.options[question.answer]);
if (question.options[question.answer].name == ans) {
console.log(ans);
scope.score++;
scope.question[scope.id].answerMode = false;
scope.answerMode = false;
scope.id++;
scope.getQuestion();
}
};
}
}
});
app.factory('quizFactory', function() {
var questions = [{
question: "Which is the largest country in the world by population?",
options: [{
name: "India",
selected: false
}, {
name: "USA",
selected: false
}, {
name: "China",
selected: false
}, {
name: "Russia",
selected: false
}],
answer: 2,
trueQId: 1,
falseQid: 3,
answers: ""
}, {
question: "When did the second world war end?",
options: [{
name: "1945",
selected: false
}, {
name: "1934",
selected: false
}, {
name: "1993",
selected: false
}, {
name: "2002",
selected: false
}],
answer: 0,
trueQId: 2,
falseQid: 3
}, {
question: "Which was the first country to issue paper currency?",
options: [{
name: "USA",
selected: false
}, {
name: "France",
selected: false
}, {
name: "Italy",
selected: false
}, {
name: "China",
selected: false
}],
answer: 3,
trueQId: 3,
falseQid: 4
}];
return {
getQuestion: function(id) {
console.log(questions);
if (id < questions.length) {
return questions[id];
} else {
return false;
}
},
updateQuestion: function(id, ans) {
questions[id].answers = ans;
}
};
});
答案 0 :(得分:0)
更改您的checkAnswer功能
scope.checkAnswer = function(question) {
console.log(question);
var options = question.options;
for (var i = 0; i < options.length; i++) {
if (options[i].selectedRadio) {
var ans = options[i].selectedRadio;
}
}
console.log(question.options[question.answer]);
if (question.options[question.answer].name == ans) {
console.log(scope.id);
scope.score++;
scope.question[scope.id].answerMode = false;
scope.answerMode = false;
scope.id++;
scope.getQuestion();
}else{
scope.question=scope.question.slice(0,question.trueQId);
console.log(scope.question);
scope.id=question.falseQid-1;
scope.getQuestion();
}
};
它会让你理解,怎么做..谢谢