mySearch.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. angular.module('push')
  2. .controller('MySearchCtrl', function ($scope, $state, $timeout, $ionicTabsDelegate, $http) {
  3. //本地获取历史搜索记录
  4. $scope.searchKeyList = JSON.parse(localStorage.getItem("searchKeyList"));
  5. if(angular.isUndefined($scope.searchKeyList) || $scope.searchKeyList == null ){
  6. $scope.searchKeyList = [];
  7. $scope.showHistory = false;
  8. }else {
  9. $scope.showHistory = true;
  10. }
  11. //热门搜索
  12. $scope.HotWordSearch = [
  13. {
  14. id: 2,
  15. word: "节能"
  16. },
  17. {
  18. id: 3,
  19. word: "环境"
  20. },
  21. {
  22. id: 4,
  23. word: "科技"
  24. },
  25. {
  26. id: 5,
  27. word: "军事"
  28. },
  29. {
  30. id: 6,
  31. word: "体育"
  32. },
  33. {
  34. id: 7,
  35. word: "健康"
  36. },
  37. {
  38. id: 8,
  39. word: "财经"
  40. },
  41. {
  42. id: 9,
  43. word: "汽车"
  44. },
  45. {
  46. id: 10,
  47. word: "社会"
  48. },
  49. {
  50. id: 11,
  51. word: "国际"
  52. }
  53. ];
  54. //删除单个历史记录
  55. $scope.onItemDelete = function (key) {
  56. $scope.searchKeyList.splice($scope.searchKeyList.indexOf(key), 1);
  57. var str = JSON.stringify($scope.searchKeyList);
  58. localStorage.setItem("searchKeyList",str);
  59. };
  60. //删除全部历史记录
  61. $scope.onItemDeleteAll = function () {
  62. $scope.searchKeyList = [];
  63. $scope.showHistory = false;
  64. var str = JSON.stringify($scope.searchKeyList);
  65. localStorage.setItem("searchKeyList",str);
  66. };
  67. //提交搜索关键词跳转到搜索结果页面
  68. $scope.SearchWordSubmit = function (key) {
  69. $timeout(function () {
  70. if(angular.isDefined(key) && key.length > 0){
  71. var word = angular.copy(key);
  72. var index = $scope.searchKeyList.indexOf(word);
  73. if(index == -1){
  74. if($scope.searchKeyList.length >= 5){
  75. $scope.searchKeyList.pop();
  76. $scope.searchKeyList.unshift(word);
  77. }else {
  78. $scope.searchKeyList.unshift(word);
  79. }
  80. }else {
  81. $scope.searchKeyList.splice(index, 1);
  82. $scope.searchKeyList.unshift(word);
  83. }
  84. }
  85. var str = JSON.stringify($scope.searchKeyList);
  86. localStorage.setItem("searchKeyList",str);
  87. },50);
  88. //跳转搜索结果页
  89. $state.go('SearchResult', {"key": key});
  90. };
  91. $scope.goSearchResult = function (key) {
  92. $state.go('SearchResult', {"key": key});
  93. };
  94. $scope.searchCont = {};
  95. //清空搜索内容
  96. $scope.clearSearch = function () {
  97. $scope.searchCont.key = "";
  98. $scope.showHistory = true;
  99. };
  100. //监测key变化,即时匹配词库
  101. $scope.fixKey = function () {
  102. if (angular.isDefined($scope.searchCont.key) && $scope.searchCont.key.length > 0) {
  103. $scope.searchKey($scope.searchCont.key);
  104. }
  105. };
  106. $scope.data = [];
  107. //百度词库
  108. $scope.searchKey = function (searchName) {
  109. var search_history = angular.element(document.getElementById("his_search"));
  110. if (search_history.val().length != 0) {
  111. var baidu = angular.element(document.getElementById("baidu_search"));
  112. baidu.css("display", "block");
  113. }
  114. $http({
  115. method: 'JSONP',
  116. url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + searchName + '&cb=JSON_CALLBACK'
  117. }).success(function (data) {
  118. $scope.data = data.s;
  119. });
  120. };
  121. $scope.gobackback = function () {
  122. $timeout(function () {
  123. $scope.goback();
  124. },300)
  125. }
  126. });