【发布时间】:2017-10-31 22:03:41
【问题描述】:
我只有 AngularJS 的基本经验。
这是一个 AngularJS 脚本,它可以从给定的国家列表中自动完成一个文本框。但我需要它从本地 Json 文件中读取数据。
完全搞糊涂了,因为我的 Json 文件包含嵌套的 Json 对象。 当我在文本框中搜索“AAL 或 Aalb”时,我需要将 AAL-AALBORG 列为列表。
有人可以简要解释一下我该怎么做吗?
<script>
var app = angular.module("myapp", []);
app.controller("usercontroller", function ($scope) {
$scope.countryList = ["USA", "UK", "INDIA","KOREA"];
$scope.complete = function (string) {
var output = [];
angular.forEach($scope.countryList, function (country) {
if (country.toLowerCase().indexOf(string.toLowerCase()) >= 0)
{
output.push(country);
}
});
$scope.filterCountry = output;
}
$scope.fillTextbox = function (string) {
$scope.country = string;
$scope.hidethis = true;
}
});
</script>
HTML 是,
<div ng-app="myapp" ng-controller="usercontroller">
<label>Enter a country</label>
<input type="text" name="country" id="country" ng-model="country" ng-keyup="complete(country)" class="form-control"/>
<ul class="list-group" ng-model="hidethis" ng-hide="hidethis">
<li class="list-group-item" ng-repeat="countrydata in filterCountry" ng-click="fillTextbox(countrydata)" >{{countrydata}}</li>
</ul>
</div>
嵌套的Json格式为:
{"AAL":{"id":"32313","airport_name":"Aalborg","latitude":"57.1","longitude":"9.85","timezone":"2","dst_indicator":"E","city":"Aalborg","country":"Denmark","country_code":"DK","region":"TC1","listing_display":"true","pseudonyms":""},"AAR":{"id":"32314","airport_name":"Tirstrup","latitude":"56.15","longitude":"10.2167","timezone":"2","dst_indicator":"E","city":"Aarhus","country":"Denmark","country_code":"DK","region":"TC1","listing_display":"true","pseudonyms":""}}
【问题讨论】:
标签: javascript angularjs json