angular.init.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. angular.module('app', [ 'ngSanitize']);
  3. angular.module('app', ['myModule']);
  4. var app = angular.module('app').config([ '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', function($controllerProvider, $compileProvider, $filterProvider, $provide) {
  5. app.controller = $controllerProvider.register;
  6. app.directive = $compileProvider.directive;
  7. app.filter = $filterProvider.register;
  8. app.factory = $provide.factory;
  9. app.service = $provide.service;
  10. app.constant = $provide.constant;
  11. app.value = $provide.value;
  12. } ]);
  13. app.factory('frameAjaxInterceptor', function($rootScope, $q, $location) {
  14. return {
  15. 'request' : function(config) {
  16. // 判断是否弹出等待框
  17. var wait = config.wait || 0;
  18. if (wait > 0) {
  19. // todo 显示提交等待框
  20. }
  21. return config;
  22. },
  23. 'requestError' : function(rejection) {
  24. return $q.reject(rejection);
  25. },
  26. 'response' : function(response) {
  27. var wait = (response.config || {}).wait || 0;
  28. // todo 隐藏等待框
  29. if (response.data.success == undefined)
  30. return response;
  31. if (response.data.success == false) {
  32. // todo 用更友好的提示框显示
  33. alert(response.data.errorMsg);
  34. if (response.config.error) {
  35. response.config.error.call(undefined);
  36. }
  37. return $q.reject(response);
  38. }
  39. return response;
  40. },
  41. 'responseError' : function(rejection) {
  42. var wait = (rejection.config || {}).wait || 0;
  43. if (wait > 0) {
  44. // todo 隐藏等待框
  45. }
  46. // todo 用更友好的提示框显示
  47. alert('无法访问服务器!');
  48. if (rejection.config.error) {
  49. rejection.config.error.call(undefined);
  50. }
  51. return $q.reject(rejection);
  52. }
  53. };
  54. }).config([ '$httpProvider', function($httpProvider) {
  55. // 设置http默认信息
  56. $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
  57. $httpProvider.defaults.headers.post['Accept'] = 'application/json, text/javascript, */*; q=0.01';
  58. $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
  59. $httpProvider.defaults.transformRequest = [ function(data) {
  60. var param = function(obj) {
  61. var query = '';
  62. var name, value, fullSubName, subName, subValue, innerObj, i;
  63. for (name in obj) {
  64. value = obj[name];
  65. if (value instanceof Array) {
  66. for (i = 0; i < value.length; ++i) {
  67. subValue = $.trim(value[i]);
  68. fullSubName = name + '[]';
  69. innerObj = {};
  70. innerObj[fullSubName] = subValue;
  71. query += param(innerObj) + '&';
  72. }
  73. } else if (value instanceof Date) {
  74. query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value.toJSON())) + '&';
  75. } else if (value instanceof Object) {
  76. for (subName in value) {
  77. subValue = $.trim(value[subName]);
  78. fullSubName = name + '[' + subName + ']';
  79. innerObj = {};
  80. innerObj[fullSubName] = subValue;
  81. query += param(innerObj) + '&';
  82. }
  83. } else if (value !== undefined && value !== null) {
  84. query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value)) + '&';
  85. }
  86. }
  87. return query.length ? query.substr(0, query.length - 1) : query;
  88. };
  89. return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
  90. } ];
  91. $httpProvider.interceptors.push('frameAjaxInterceptor');
  92. } ]);