我正在访问地址查找 REST API 服务 - JSON 响应。我想显示我的 JSON 响应,例如自动下拉列表。 Rest 服务结果有 5 个 JSON 数组列表用于我的所有搜索,但在下面的代码中,JSON 结果再次根据文本框中输入的值进行过滤。
http://jsfiddle.net/3etbtfwL/230/
测试用例# 1) 在文本框中输入“google”,休息服务返回 5 个 JSON 数组列表,在文本框中显示自动填充结果,如下所示 - 通过
2) 在文本框中输入“google Drive” - 休息服务实际上正确返回 5 个 JSON 数组列表,但文本框自动填充列表中没有显示任何值 - 失败 (原因 - 我输入的值与 JSON 返回列表值不匹配)
如何显示从 JSON 结果返回的所有值,例如文本框自动填充。
Angular 代码
var app = angular.module('httpApp', []);
app.controller('httpAppCtrlr', function ($scope, $http) {
$scope.AddressLookup = function (address) {
$http.get('http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/suggest?text=' + address + '&f=json')
.then(function (results) {
$scope.items = results.data.suggestions;
});
}
});
HTML 代码
<div data-ng-app='httpApp' data-ng-controller='httpAppCtrlr'>
<input type="text" data-ng-model="item.text" list="comicstitle"
placeholder="Address Lookup"
ng-change="AddressLookup(item.text)"
>
<datalist id="comicstitle">
<option ng-repeat="item in items" value="{{item.text}}" >{{item.text}}</option>
</datalist>
</div>
最佳答案
您是否收到有关您输入的文本Google Drive的响应,如果是这样,请在 Controller 内使用 $timeout
var app = angular.module('httpApp', []);
app.controller('httpAppCtrlr', function ($scope, $http, $timeout) {
$scope.AddressLookup = function (address) {
$http.get('http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/suggest?text=' + address + '&f=json')
.then(function (results) {
$timeout(function() {
$scope.items = results.data.suggestions;
})
});
}
});
关于jquery - 显示其余 api json 响应,例如使用 Angular js 自动填充文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959699/