angular.module('push') .controller('MySearchCtrl', function ($scope, $state, $timeout, $ionicTabsDelegate, $http) { //本地获取历史搜索记录 $scope.searchKeyList = JSON.parse(localStorage.getItem("searchKeyList")); if(angular.isUndefined($scope.searchKeyList) || $scope.searchKeyList == null ){ $scope.searchKeyList = []; $scope.showHistory = false; }else { $scope.showHistory = true; } //热门搜索 $scope.HotWordSearch = [ { id: 2, word: "节能" }, { id: 3, word: "环境" }, { id: 4, word: "科技" }, { id: 5, word: "军事" }, { id: 6, word: "体育" }, { id: 7, word: "健康" }, { id: 8, word: "财经" }, { id: 9, word: "汽车" }, { id: 10, word: "社会" }, { id: 11, word: "国际" } ]; //删除单个历史记录 $scope.onItemDelete = function (key) { $scope.searchKeyList.splice($scope.searchKeyList.indexOf(key), 1); var str = JSON.stringify($scope.searchKeyList); localStorage.setItem("searchKeyList",str); }; //删除全部历史记录 $scope.onItemDeleteAll = function () { $scope.searchKeyList = []; $scope.showHistory = false; var str = JSON.stringify($scope.searchKeyList); localStorage.setItem("searchKeyList",str); }; //提交搜索关键词跳转到搜索结果页面 $scope.SearchWordSubmit = function (key) { $timeout(function () { if(angular.isDefined(key) && key.length > 0){ var word = angular.copy(key); var index = $scope.searchKeyList.indexOf(word); if(index == -1){ if($scope.searchKeyList.length >= 5){ $scope.searchKeyList.pop(); $scope.searchKeyList.unshift(word); }else { $scope.searchKeyList.unshift(word); } }else { $scope.searchKeyList.splice(index, 1); $scope.searchKeyList.unshift(word); } } var str = JSON.stringify($scope.searchKeyList); localStorage.setItem("searchKeyList",str); },50); //跳转搜索结果页 $state.go('SearchResult', {"key": key}); }; $scope.goSearchResult = function (key) { $state.go('SearchResult', {"key": key}); }; $scope.searchCont = {}; //清空搜索内容 $scope.clearSearch = function () { $scope.searchCont.key = ""; $scope.showHistory = true; }; //监测key变化,即时匹配词库 $scope.fixKey = function () { if (angular.isDefined($scope.searchCont.key) && $scope.searchCont.key.length > 0) { $scope.searchKey($scope.searchCont.key); } }; $scope.data = []; //百度词库 $scope.searchKey = function (searchName) { var search_history = angular.element(document.getElementById("his_search")); if (search_history.val().length != 0) { var baidu = angular.element(document.getElementById("baidu_search")); baidu.css("display", "block"); } $http({ method: 'JSONP', url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + searchName + '&cb=JSON_CALLBACK' }).success(function (data) { $scope.data = data.s; }); }; $scope.gobackback = function () { $timeout(function () { $scope.goback(); },300) } });