angular.module('push') .controller('ConnSearchCtrl', function ($scope, $state, $timeout, $ionicTabsDelegate, $http, $ionicHistory, $stateParams) { //本地获取历史搜索记录 $scope.searchKeyList = JSON.parse(localStorage.getItem("connSearchKeyList")); if (angular.isUndefined($scope.searchKeyList) || $scope.searchKeyList == null) { $scope.searchKeyList = []; $scope.showHistory = false; } else { $scope.showHistory = true; } //删除单个历史记录 $scope.onItemDelete = function (key) { $scope.searchKeyList.splice($scope.searchKeyList.indexOf(key), 1); var str = JSON.stringify($scope.searchKeyList); localStorage.setItem("connSearchKeyList", str); }; //删除全部历史记录 $scope.onItemDeleteAll = function () { $scope.searchKeyList = []; $scope.showHistory = false; var str = JSON.stringify($scope.searchKeyList); localStorage.setItem("connSearchKeyList", 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("connSearchKeyList", str); }, 50); //跳转搜索结果页 $ionicHistory.clearCache(["connsearchresult"]); $state.go('connsearchresult', { key: key, folderid: $stateParams.folderid, localfolderid: $stateParams.localfolderid }); }; $scope.goSearchResult = function (key) { $ionicHistory.clearCache(["connsearchresult"]); $state.go('connsearchresult', { key: key, folderid: $stateParams.folderid, localfolderid: $stateParams.localfolderid }); }; $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) } });