Advertisement
blfuentes

quiz-controller

Aug 20th, 2014
487
0
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. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement