jquery - 显示其余 api json 响应,例如使用 Angular js 自动填充文本框

标签 jquery angularjs json rest api

我正在访问地址查找 REST API 服务 - JSON 响应。我想显示我的 JSON 响应,例如自动下拉列表。 Rest 服务结果有 5 个 JSON 数组列表用于我的所有搜索,但在下面的代码中,JSON 结果再次根据文本框中输入的值进行过滤。

http://jsfiddle.net/3etbtfwL/230/

测试用例# 1) 在文本框中输入“google”,休息服务返回 5 个 JSON 数组列表,在文本框中显示自动填充结果,如下所示 - 通过

enter image description here

2) 在文本框中输入“google Drive” - 休息服务实际上正确返回 5 个 JSON 数组列表,但文本框自动填充列表中没有显示任何值 - 失败 (原因 - 我输入的值与 JSON 返回列表值不匹配)

enter image description here

如何显示从 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/

相关文章:

jQuery sibling 标签 "a"

javascript - 如何检查浏览器是否在 MAC 上运行?

javascript - (Angular.js) 如何通过过滤器计算数组中有多少项?

javascript - AngularJS:http 链调用问题和 NodeJS:在多个 http 调用中使用变量的问题

javascript - 使用 javascript 和 Angularjs 循环

javascript - JSON 元素选择

jquery - jQuery 可以仅针对顶层执行复合选择吗? (又名 "How to avoid chaining children")

javascript - 如何使用jQuery更新数据库而不刷新页面?

javascript - C# json解析格式

Java Selenium 获取 JSON 响应正文