多项选择Angular App-显示答案 -

时间:2016-01-07 03:53:22

标签: javascript jquery html angularjs

我使用AngularJS开发了单页多选测验Web应用程序。我试图从数组列表中获得正确的答案,以便在用户点击标签内的提交按钮后选择错误的答案。我知道这很简单,我知道我正在分析它,使它看起来比它不复杂。我所做的每一次尝试都会显示数组列表中的所有选项,或者数组列表中的正确数字位置值,而不是单个正确答案。我也是使用AngularJS的新手。

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>

模板

<div class="quiz-area" ng-show="inProgress">
<div ng-show="!quizOver">
    <h2 id="question">{{question}}</h2>
    <ul id="options">
        <li ng-repeat="option in options">
            <label>
                <input type="radio" name="answer" value="{{option}}">
                {{option}}
            </label>
        </li>
    </ul>
    <button ng-click="checkAnswer()" ng-show="answerMode">Submit</button>

            <div ng-show="!answerMode">
        <button ng-click="nextQuestion()" class="next-question">Next</button>
        <span ng-show="correctAns">That is correct!</span>
        <span ng-show="!correctAns">Sorry, that is an incorrect answer.</span>
    </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>

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.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);
            if(q) {
                scope.question = q.question;
                scope.options = q.options;
                scope.answer = q.answer;
                scope.answerMode = true;
            } else {
                scope.quizOver = true;
            }
        };

        scope.checkAnswer = function() {
            if(!$('input[name=answer]:checked').length) return;

            var ans = $('input[name=answer]:checked').val();

            if(ans == scope.options[scope.answer]) {
                scope.score++;
                scope.correctAns = true;
            } else {
                scope.correctAns = false;
            }

            scope.answerMode = false;
        };

        scope.nextQuestion = function() {
            scope.id++;
            scope.getQuestion();
        }

        scope.reset();
    }
}
});

app.factory('quizFactory', function() {
var questions = [
    {
        question: "Which is the largest country in the world by population?",
        options: ["India", "USA", "China", "Russia"],
        answer: 2
    },
    {
        question: "When did the second world war end?",
        options: ["1945", "1939", "1944", "1942"],
        answer: 0
    },
    {
        question: "Which was the first country to issue paper currency?",
        options: ["USA", "France", "Italy", "China"],
        answer: 3
    },
    {
        question: "Which city hosted the 1996 Summer Olympics?",
        options: ["Atlanta", "Sydney", "Athens", "Beijing"],
        answer: 0
    },
    {   
        question: "Who invented telephone?",
        options: ["Albert Einstein", "Alexander Graham Bell", "Isaac Newton", "Marie Curie"],
        answer: 1
    }
];

return {
    getQuestion: function(id) {
        if(id < questions.length) {
            return questions[id];
        } else {
            return false;
        }
    }
};
});

0 个答案:

没有答案