我一直在练习AngularJS1x做Lynda和Udemy的教程。我做了一个创建多项选择测验的教程。我想看看我是否通过重新创建它来理解代码,然后将其推得更远但是试图填充空白测验。
问题和答案全部来自JSON文件。填充检查正常工作,如果用户输入匹配JSON对象中的字符串,它可以正常工作。我没有做太多改变但是在尝试调用$ parent.index数组时代码不起作用。有关我用作多项选择测验的原始代码的参考,您可以找到该链接here
我很乐意,如果有人能够解释为什么我错了,如果我错误地设置它,那么我会变得更好。我将它与原始代码进行了比较,无法找到我出错的地方。
问题在于定位对象的数组编号。我还将展示下面的代码。
问题 它位于调用qIndex参数的任何内容中,如下所示:
$scope.myQuestions[qIndex].questionState; //Says it is undefined
如果我给它一个像0这样的硬编码数组,那就可以了
$scope.myQuestions[0].questionState //If all of them are like this its fine as it calls the first question and finds the correct answer if typed into the input.
以下是代码:
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($parent.$index, $index)}"
ng-click="checkAnswer($parent.$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 class="results {{ (totalQuestions === activeQuestion) ? 'active' : 'inactive' }}">
<div>
<h3>Results</h3>
<p>You Scored {{percentage}}% by correctly answering {{score}} of the total {{totalQuestions}} questions.</p>
<p>Use the links below to challenge your friends.</p>
<div class="share" ng-bind-html="createShareLinks(percentage)"></div>
</div>
</div>
</div>
QUIZ.JS
(function(){
var codeApp = angular.module('codeApp', ['ngSanitize']);
codeApp.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();
console.log(aIndex); //logs 0
console.log(qIndex); //logs undefined
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!');
}
$scope.myQuestions[qIndex].questionState = 'answered';
}else{
console.log('Something is wrong');
}
$scope.isCorrect = function(qIndex,aIndex){
return $scope.myQuestions[qIndex].questionAnswer === userAnswer;
}
}
$scope.selectContinue = function(){
return $scope.activeQuestion += 1;
}
$scope.createShareLinks = function(percentage){
var url = 'http://codifydesign.com';
var emailLink = '<input type="text" placeholder="hi" /><a class="btn email" href="mailto:?subject=Try to beat my quiz score!&body=I scored a '+percentage+'% on this quiz about Saturn. Try to beat my score at '+url+'">Email a friend</a>';
var twitterLink = '<a class="btn twitter" target="_blank" href="http://twitter.com/share?text=I scored a '+percentage+'% on this quiz about Saturn. Try to beat my score at&hashtags=SaturnQuiz&url='+url+'">Tweet your score</a>';
var newMarkup = emailLink + twitterLink;
return $sce.trustAsHtml(newMarkup);
}
}]).filter('trustAsHtml', [
'$sce',
function($sce) {
return function(value) {
return $sce.trustAsHtml(value);
}
}
]);
})();
JSON
[
{
"questionId": 0,
"question" : "Saturn is <input id='guess-input' class='fillin' type='text' name='\"six\"'> many planets from the sun?",
"questionAnswer" : "six"
},
{
"questionId": 1,
"question" : "Around Saturn are <input id='guess-input' class='fillin' type='text' name='\"rings\"'>",
"questionAnswer" : "rings"
}
]
答案 0 :(得分:1)
在angularjs $parent.$index
中,来自父作用域的$index
。当你有嵌套循环时它很有用。然后,您可以使用$index
构造从父作用域获取$parent.$index
。您的代码没有嵌套循环。 ng-repeat="myQuestion in myQuestions"
是最高级别的循环。因此它的父作用域中没有索引。我可以猜测,在原始代码中,另一个循环在另一个ng-repeat
中有一个ng-repeat
。