123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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)
- }
- });
|