You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.6 KiB

  1. angular.module('starter.controllers', [])
  2. .controller('AppCtrl', function($scope, $ionicModal, $timeout) {
  3. // With the new view caching in Ionic, Controllers are only called
  4. // when they are recreated or on app start, instead of every page change.
  5. // To listen for when this page is active (for example, to refresh data),
  6. // listen for the $ionicView.enter event:
  7. //$scope.$on('$ionicView.enter', function(e) {
  8. //});
  9. // Form data for the login modal
  10. $scope.loginData = {};
  11. // Create the login modal that we will use later
  12. $ionicModal.fromTemplateUrl('templates/login.html', {
  13. scope: $scope
  14. }).then(function(modal) {
  15. $scope.modal = modal;
  16. });
  17. // Triggered in the login modal to close it
  18. $scope.closeLogin = function() {
  19. $scope.modal.hide();
  20. };
  21. // Open the login modal
  22. $scope.login = function() {
  23. $scope.modal.show();
  24. };
  25. // Perform the login action when the user submits the login form
  26. $scope.doLogin = function() {
  27. console.log('Doing login', $scope.loginData);
  28. // Simulate a login delay. Remove this and replace with your login
  29. // code if using a login system
  30. $timeout(function() {
  31. $scope.closeLogin();
  32. }, 1000);
  33. };
  34. })
  35. .controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout) {
  36. $scope.travels="";
  37. $http.get('http://localhost:3000/api/travels')
  38. .success(function(data, status, headers,config){
  39. console.log('data success');
  40. console.log(data); // for browser console
  41. $scope.travels = data; // for UI
  42. })
  43. .error(function(data, status, headers,config){
  44. console.log('data error');
  45. })
  46. .then(function(result){
  47. travels = result.data;
  48. });
  49. $scope.newtravel={};
  50. // Create the login modal that we will use later
  51. $ionicModal.fromTemplateUrl('templates/newtravel.html', {
  52. scope: $scope
  53. }).then(function(modal) {
  54. $scope.modal = modal;
  55. });
  56. // Triggered in the login modal to close it
  57. $scope.closeNewTravel = function() {
  58. $scope.modal.hide();
  59. };
  60. // Open the login modal
  61. $scope.showNewTravel = function() {
  62. $scope.modal.show();
  63. };
  64. // Perform the login action when the user submits the login form
  65. $scope.doNewTravel = function() {
  66. console.log('Doing new travel', $scope.newtravel);
  67. $scope.newtravel.icon="lorry";
  68. $scope.newtravel.generateddate=$scope.newtravel.date;
  69. $scope.newtravel.owner="user";
  70. console.log($scope.newtravel);
  71. $http({
  72. url: 'http://localhost:3000/api/travels',
  73. method: "POST",
  74. data: $scope.newtravel
  75. })
  76. .then(function(response) {
  77. // success
  78. console.log("response: ");
  79. console.log(response);
  80. $scope.newtravel._id=response.data._id;
  81. $scope.travels.push($scope.newtravel);
  82. },
  83. function(response) { // optional
  84. // failed
  85. });
  86. // Simulate a login delay. Remove this and replace with your login
  87. // code if using a login system
  88. $timeout(function() {
  89. $scope.closeNewTravel();
  90. }, 1000);
  91. };
  92. })
  93. .controller('TravelCtrl', function($scope, $stateParams, $http) {
  94. $scope.travel="";
  95. console.log($stateParams.travelId);
  96. $http.get('http://localhost:3000/api/travels/'+$stateParams.travelId)
  97. .success(function(data, status, headers,config){
  98. console.log('data success');
  99. console.log(data); // for browser console
  100. $scope.travel = data; // for UI
  101. })
  102. .error(function(data, status, headers,config){
  103. console.log('data error');
  104. })
  105. .then(function(result){
  106. travels = result.data;
  107. });
  108. });