controllers.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. angular.module('starter.controllers', [])
  2. .controller('DashCtrl', function($scope) {})
  3. .controller('ChatsCtrl', function($scope, Chats) {
  4. // With the new view caching in Ionic, Controllers are only called
  5. // when they are recreated or on app start, instead of every page change.
  6. // To listen for when this page is active (for example, to refresh data),
  7. // listen for the $ionicView.enter event:
  8. //
  9. //$scope.$on('$ionicView.enter', function(e) {
  10. //});
  11. $scope.chats = Chats.all();
  12. $scope.remove = function(chat) {
  13. Chats.remove(chat);
  14. };
  15. })
  16. .controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
  17. $scope.chat = Chats.get($stateParams.chatId);
  18. })
  19. .controller('AccountCtrl', function($scope,$resource,$state,Task) {
  20. $scope.time = new Date();
  21. var task = $resource('http://192.168.1.64:8000/task/:id/');
  22. task.get(function (res) {
  23. $scope.taskList = res;
  24. }, function (err) {
  25. alert(JSON.stringify(err));
  26. $scope.taskList = [{
  27. id:1,title:'任务',description:'123456',created:new Date(),is_completed:false
  28. }]
  29. })
  30. $scope.toAdd = function () {
  31. $state.go('tab.task-detail',{
  32. id:0 //增加页面时候,浏览器中最后的参数值
  33. })
  34. }
  35. $scope.toDetail = function (item) {
  36. Task.detail = _.clone(item);
  37. $state.go('tab.task-detail',{
  38. id:item.id
  39. })
  40. }
  41. $scope.doRefresh = function () {
  42. getData();
  43. }
  44. function getData() {
  45. Task.getTask.get(function (res) {
  46. $scope.taskList = res;
  47. }, function (err) {
  48. alert(JSON.stringify(err));
  49. }).$promise.finally(function () {
  50. $scope.$broadcast('scroll.refreshComplete');
  51. });
  52. }
  53. })
  54. .controller('TaskDetailCtrl',function ($scope,$stateParams,$ionicHistory,$resource,Task) {
  55. var id = $stateParams.id;
  56. var isNew = id == 0 ? true : false;
  57. $scope.data = isNew ? {
  58. title:null,
  59. description:null
  60. } : _.clone(Task.detail);
  61. var task = $resource('http://192.168.1.64:8000/task/:id/');
  62. $scope.addTask = function () {
  63. console.log($scope.data);
  64. if (isNew) {
  65. task.save($scope.data,function () {
  66. $ionicHistory.goBack();
  67. })
  68. } else {
  69. Task.getTask.get($scope.data,function () {
  70. $ionicHistory.goBack();
  71. })
  72. }
  73. };
  74. $scope.deleteTask = function () {
  75. Task.getTask.delete({id:id},function(){
  76. $ionicHistory.goBack();
  77. })
  78. }
  79. $scope.comleted =function () {
  80. if(!$scope.data.is_completed) $scope.data.is_completed = !$scope.data.is_completed;
  81. }
  82. });