angularjs - checking values against an array -
i newbie struggling angular js.
i trying make questionaire , have following code keep getting feedback incorrect no matter try.
i have assigned numeric value each of 4 buttons, titles of being pulled through array. want check value of selected button against correct answer in array. can tell me going wrong?
html <div id="ansblock"> <button type="button" ng-click="myanswer=0">{{options [0]}} </button> <button type="button" ng-click="myanswer=1">{{options [1]}}</button> <button type="button" ng-click="myanswer=2">{{options [2]}}</button> <button type="button" ng-click="myanswer=3">{{options [3]}}</button> <p>test show button click value working {{myanswer}}</p> </div> <button ng-click="checkanswer()">submit</button> <span ng-show="correctans">that correct!</span> <span ng-show="!correctans">sorry, incorrect answer.</span> 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 () { var ans = $('scope.myanswer').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 of following ww2 fighter?", options: ["albatross", "concorde", "spitfire", "mirage"], answer: 2 }, { question: "when great fire of london?", options: ["1666", "1966", "1844", "1235"], answer: 0 }, { question: "which group swedish pop band?", options: ["eagles", "led zeppelin", "beatles", "abba"], answer: 3 }, { question: "which city capita of australia?", options: ["canberra", "sydney", "melbourne", "hobart"], answer: 0 }, { question: "which brand not make motorcycles?", options: ["bmw", "mercedes", "kawasaki", "honda"], answer: 1 } ]; return { getquestion: function (id) { if (id < questions.length) { return questions[id]; } else { return false; } } }; });
Comments
Post a Comment