app.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. var lab = angular.module('lab', []);
  3. lab.controller('LabCtrl', function ($scope, $http, $timeout) {
  4. $scope.noun1 = "";
  5. $scope.noun2 = "";
  6. $scope.adjective1 = "";
  7. $scope.adjective2 = "";
  8. $scope.verb = "";
  9. getWord($http, $timeout, '/words/noun', function(resp) {
  10. $scope.noun1 = word(resp);
  11. });
  12. getWord($http, $timeout, '/words/noun', function(resp) {
  13. $scope.noun2 = word(resp);
  14. });
  15. getWord($http, $timeout, '/words/adjective', function(resp) {
  16. var adj = word(resp);
  17. adj.word = adj.word.charAt(0).toUpperCase() + adj.word.substr(1)
  18. $scope.adjective1 = adj;
  19. });
  20. getWord($http, $timeout, '/words/adjective', function(resp) {
  21. $scope.adjective2 = word(resp);
  22. });
  23. getWord($http, $timeout, '/words/verb', function(resp) {
  24. $scope.verb = word(resp);
  25. });
  26. });
  27. function getWord($http, $timeout, url, callback) {
  28. $http.get(url).then(callback, function(resp) {
  29. $timeout(function() {
  30. console.log("Retry: " + url);
  31. getWord($http, $timeout, url, callback);
  32. }, 500);
  33. });
  34. }
  35. function word(resp) {
  36. return {
  37. word: resp.data.word,
  38. hostname: resp.headers()["source"]
  39. };
  40. }