首先,我不太瞭解angularjs。我確實設法使用angularjs將單個文件網頁拼湊在一起。我作爲顯示在下面的時候遇到麻煩1.3.15版本轉換$ HTTP調用到1.6.4版本:
var url = 'http://suggestqueries.google.com/complete/search?callback=JSON_CALLBACK&client=firefox&hl=en&q=' + encodeURIComponent($scope.searchText);
$http.defaults.useXDomain = true;
$http({
url: url,
method: 'JSONP',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).
success(function(data, status, headers, config) {
$scope.suggestions = data[1];
}).
error(function(data, status, headers, config) {
$scope.suggestions = ['error connecting'];
});
不是真正確定它應該是什麼樣子。
這裏是整個文件:
<!DOCTYPE html>
<html>
<head>
<script data-require="[email protected]" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
<style>
#appDiv
{
position: fixed;
top: 30%;
left: 80%;
transform: translate(-80%, 0%);
width:50%;
}
#entry
{
width: 100%
}
#searchInput
{
display: table-cell;
color: #808080;
width:100%;
font-size:150%;
}
.goButton
{
font-size:150%;
}
.list
{
list-style-type: none;
margin: 0;
padding: 0;
cursor: default;
border-style: solid;
border-width: 1px;
border-color: #DFDFDF;
}
.list:empty
{
border-style: none;
}
.listItem
{
color: #404040;
font-size:120%;
}
.listItem:hover
{
background: #DFDFDF;
}
</style>
</head>
<body>
<div id="appDiv" ng-app="googleSearch" ng-controller="googleCtrl">
<form method="get" action="http://www.google.com/search" autocomplete="off">
<table border="0" align="center" cellpadding="0">
<tr>
<td id="entry">
<input type="text" name="q" id="searchInput" autofocus="autofocus" maxlength="255" ng-model="searchText" ng-keyup="search()" />
</td>
<td>
<input class="goButton" type="submit" value=" Go! "/>
<input type="hidden" name="sitesearch" value="" />
</td>
</tr>
<tr>
<td colspan="2" ng-mouseleave="restore()">
<ul class="list"><li class="listItem" ng-repeat="x in suggestions" ng-click="select(x)" ng-mouseover="preview(x)">{{x}}</li></ul>
</td>
</tr>
</table>
</form>
</div>
<script>
var app = angular.module("googleSearch", []);
app.controller("googleCtrl", function($scope, $http)
{
$scope.select = function(text)
{
$scope.searchText = text;
$scope.memory = text;
$scope.suggestions = [];
document.getElementById("searchInput").focus();
}
$scope.preview = function(text)
{
$scope.searchText = text;
}
$scope.restore = function()
{
$scope.searchText = $scope.memory;
}
$scope.search = function()
{
$scope.memory = $scope.searchText;
googleSearch();
}
googleSearch = function()
{
if ($scope.searchText == null || $scope.searchText.length < 1)
{
$scope.suggestions = [];
return
}
var url = 'http://suggestqueries.google.com/complete/search?callback=JSON_CALLBACK&client=firefox&hl=en&q=' + encodeURIComponent($scope.searchText);
$http.defaults.useXDomain = true;
$http({
url: url,
method: 'JSONP',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).
success(function(data, status, headers, config) {
$scope.suggestions = data[1];
}).
error(function(data, status, headers, config) {
$scope.suggestions = ['error connecting'];
});
}
});
</script>
</body>
</html>
我有,當我使用的GoogleSearch功能麻煩:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
代替:
<script data-require="[email protected]" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
在頭元件。任何建議,將不勝感激。
感謝這工作。我必須將置於頭部,然後將控制器更改爲:app.controller(「googleCtrl 「['$ scope','$ http','$ sce',函數($ scope,$ http,$ sce) – user1819780
正確...這就是爲什麼我提到包含它並注入$ sce – charlietfl
我不知道爲什麼有人投下了這個答案。 – user1819780