这些代码行的含义是什么?

时间:2017-04-30 10:07:31

标签: javascript jquery angularjs

我是一名新学员并参与测验应用参考。通过在线教程。

我有一个在线教程的测验应用程序代码。但有些代码行我不明白。

我的意思是,我不明白这些线的意义和减速。拜托,有人解释一下。如果你需要原始教程请看到这个 https://www.youtube.com/watch?v=3mTlr1Nlnc0&list=PLw5h0DiJ-9PBcL0sqrGXvSOYFncEAPObY&index=19

(function(){
var app = angular.module('myQuiz', []);
app.controller('QuizController', ['$scope', '$http', '$window', function($scope,$http, $window){
    $scope.quizSecHeight = $window.innerHeight;
    $scope.score = 0 ;
    $scope.activeQuestion = -1 ;
    $scope.activeQuestionAnswered = 0 ;
    $scope.percentage = 0 ;
    $http.get('quiz_data.html').then(function(quizData){
        $scope.myQuestions =  quizData.data;
        $scope.totalQuestions = $scope.myQuestions.length;
    });
    $scope.selectAnswer = function (qIndex, aIndex){
        var questionState = $scope.myQuestions[qIndex].questionState;
        $scope.myQuestions[qIndex].correctAnswer = correctAnswer;
        if ( questionState != 'answered' ){

            $scope.myQuestions[qIndex].selectedAnswer = aIndex;
            var correctAnswer =  $scope.myQuestions[qIndex].correct; // with isSelected
            $scope.myQuestions[qIndex].correctAnswer = correctAnswer; // with isCorrect

            if ( aIndex === correctAnswer ){
                 $scope.myQuestions[qIndex].correctness = "correct";
                 $scope.score += 1;
            } else {
                $scope.myQuestions[qIndex].correctness = "incorrect";
            }
            $scope.myQuestions[qIndex].questionState = "answered";
        }
        $scope.percentage = ($scope.score / $scope.totalQuestions)*100; 
    }
    $scope.isSelected = function(qIndex, aIndex){
        return $scope.myQuestions[qIndex].selectedAnswer === aIndex;
    }
    $scope.isCorrect = function(qIndex, aIndex){
        return $scope.myQuestions[qIndex].correctAnswer === aIndex;
    }
    $scope.selectContinue = function(){
        return $scope.activeQuestion +=1;
    }
}]);

})();

关于这段代码,我理解所有行但不理解

var questionState = $scope.myQuestions[qIndex].questionState;
$scope.myQuestions[qIndex].correctAnswer = correctAnswer;
if ( questionState != 'answered' ){

1 个答案:

答案 0 :(得分:0)

myQuestions是一个包含问题状态的范围数组(可能是状态是为了答案与否)。

我想,这些行来自一个获得参数'qIndex'(和​​correctAnser)的函数。 当用户创建问题并告知哪个答案是正确的时,可以调用此函数。

这就是我从你的问题中理解的内容。