我正在使用jQuery练习AngularJS和JavaScript。我正在尝试我的技能,我将自己的旋转添加到关于土星测验的教程中,将其更改为填空的样式测验。
我能够得到第一个答案,以确认它是对还是错,但即使答案是正确的,下一个问题总是返回错误。通过使用console.log,我发现AngularJS无法识别改变的输入值。
我甚至尝试使用onblur事件清除值,但它不起作用,并且在单击“检查答案”按钮时仍保留以前的值。我感谢您的帮助和指导,我一直在寻求改进我的编码方式!
修改: Here's a link to the Plunkr for a live demo
HTML
<div id="myQuiz" ng-controller="QuizController">
<h1>Test Your Knowledge:<span>Saturn</span></h1>
<div class="progress">
<div class="{{ ($index === activeQuestion) ? 'on' : 'off' }}
{{ (myQuestion.questionState === 'answered') ? 'answered' : 'unanswered' }}
{{ (myQuestion.correctness === 'correct') ? 'correct' : 'incorrect' }}"
ng-repeat="myQuestion in myQuestions">
</div>
</div>
<div class="intro {{ (activeQuestion > -1) ? 'inactive' : 'active' }}">
<h2>Welcome</h2>
<p>Click to begin to test your knowledge of Saturn.</p>
<p class="btn" ng-click="activeQuestion = 0">Begin</p>
</div>
<div class="question
{{ $index === activeQuestion ? 'active' : 'inactive' }}
{{ myQuestion.questionState === 'answered' ? 'answered' : 'unanswered' }}"
ng-repeat="myQuestion in myQuestions">
<p class="txt"> {{ myQuestion.instructions }} </p>
<div class="txt" ng-bind-html="myQuestion.question | trustAsHtml">
</div>
<p class="ans"
ng-class="{
correct:isCorrect($index, $index)}"
ng-click="checkAnswer($index, $index)">Check Answer
</p>
<div class="feedback">
<p ng-show="myQuestion.correctness === 'correct'"><strong>Correct</strong>.</p>
<p ng-show="myQuestion.correctness === 'incorrect'">Oops! That is not correct.</p>
<p> {{ myQuestion.feedback }} </p>
<div class="btn" ng-click="selectContinue()">Continue</div>
</div>
</div>
App.js
myApp.controller('QuizController', ['$scope', '$http', "$sce", function($scope, $http, $sce){
$scope.score = 0;
$scope.activeQuestion = -1;
$scope.activeQuestionAnswered = 0;
$scope.percentage = 0;
$http.get('quiz_data.json').then(function(quizData){
$scope.myQuestions = quizData.data;
$scope.totalQuestions = $scope.myQuestions.length;
});
$scope.checkAnswer = function(qIndex,aIndex){
var questionState = $scope.myQuestions[qIndex].questionState;
if(questionState != 'answered') {
var userAnswer = $(".fillin").val();
var correctAnswer = $scope.myQuestions[qIndex].questionAnswer;
$scope.myQuestions[qIndex].questionAnswer = correctAnswer;
if(userAnswer === correctAnswer){
$scope.myQuestions[qIndex].correctness = 'correct';
$scope.score += 1;
console.log('Correct!' + $scope.score);
}
else{
$scope.myQuestions[qIndex].correctness = 'incorrect';
console.log('Wrong!');
console.log(correctAnswer);
console.log(userAnswer); //logs previous input submitted
console.log( $(".fillin").val() ); //logs previous input submitted even though the value has changed
}
$scope.myQuestions[qIndex].questionState = 'answered';
}else{
console.log('Something is wrong');
}
}
$scope.selectContinue = function(){
return $scope.activeQuestion += 1;
}
$scope.clearValues = function(){
$(".fillin").focus(function(){
$(this).val('');
console.log('fillin value is ' + userAnswer)
});
}
data.json
[
{
"questionId": 0,
"question" : "Saturn is <input id='guess-input' class='fillin' ng-blur='clearValues()' type='text' name='\"six\"'> many planets from the sun?",
"questionAnswer" : "six"
},
{
"questionId": 1,
"question" : "Around Saturn are <input id='guess-input' class='fillin' ng-blur='clearValues()' type='text' name='\"rings\"'>",
"questionAnswer" : "rings"
}
]