SHARE
TWEET

quiz-controller

blfuentes Aug 20th, 2014 258 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = angular.module('QuizApp', [])
  2.  
  3. app.controller('QuizCtrl', ['$http', function ($http) {
  4.     this.answered = false;
  5.     this.title = "loading question...";
  6.     this.options = [];
  7.     this.correctAnswer = false;
  8.     this.working = false;
  9.  
  10.     this.answer = function () {
  11.         return this.correctAnswer ? 'correct' : 'incorrect';
  12.     };
  13.  
  14.     // GET
  15.     this.nextQuestion = function () {
  16.         this.working = true;
  17.         this.answered = false;
  18.         this.title = "loading question...";
  19.         this.options = [];
  20.  
  21.         $http.get('/api/trivia').success(function (data, status, headers, config) {
  22.             this.options = data.options;
  23.             this.title = data.title;
  24.             this.answered = false;
  25.             this.working = false;
  26.         }).error(function (data, status, headers, config) {
  27.             this.title = "Oops... something went wrong.";
  28.             this.working = false;
  29.         });
  30.     };
  31.  
  32.     // POST
  33.     this.sendAnswer = function (option) {
  34.         this.working = true;
  35.         this.answered = true;
  36.  
  37.         $http.post('/api/trivia', { 'questionId': option.questionId, 'optionId': option.id }).success(function (data, status, headers, config) {
  38.             this.correctAnswer = (data === "true");
  39.             this.working = false;
  40.         }).error(function (data, status, headers, config) {
  41.             this.title = "Oops... something went wrong.";
  42.             this.working = false;
  43.         });
  44.     };
  45. }]);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top