-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo07_2.html
54 lines (46 loc) · 1.51 KB
/
demo07_2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>模拟百度搜索</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script>
var app = angular.module('myapp', []);
app.controller('aaa', function aaa($scope,$http,$timeout) {
// var url="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=JSON_CALLBACK";
//$http.get(url).success( function(data) {
// console.log(data)
// });
var timer = null;
$scope.search = [];
$scope.change = function(name){
// 清除定时器
$timeout.cancel(timer);
timer = $timeout(function(){
$http({
method : 'JSONP',
url : 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+name+'&cb=JSON_CALLBACK'
}).success(function(data){
// console.log(data)
$scope.search = data.s; // 通过打印可以看出搜索的结果是存在了data.s里面的
});
},500)
}
});
</script>
</head>
<body ng-app="myapp">
<div ng-controller="aaa">
<!-- 在搜索框里面输入了值之后就发送请求,每输入一次就发送一次请求,这样频繁的发送不好,所以用了定时器,快速的输入内容之后再发送请求。 -->
<input type="text" ng-model="name" ng-keyup="change(name)" >
<input type="button" value="搜索" ng-click="change(name)" >
<ul>
<li ng-repeat="s in search">{{s}}</li>
</ul>
</div>
</body>
</html>