我有一个项目,我尝试使用来自http://angular-ui.github.io/bootstrap/的Typeahead(ui.bootstrap.typeahead)
我有一个来自远程服务器的选择加载数据选项(data1.json)。
当用户输入select时,我想在下拉列表中显示“Acrelandia - AC”格式(格式为cityname +' - '+ id),ng-model需要包含下一个数据: {城市: '阿克里兰迪亚',状态: 'AC'}
当然,选择的文本必须是:“Acrelandia - AC”
我尝试了下一个但是,我不知道如何根据我的要求格式化结果。它以错误的格式显示所有城市。
JS: DATA1:JSON:
{
"state": [
{
"id": "AC",
"name": "Acre",
"city": [
"Acrelandia",
"Assis Brasil",
"Brasileia",
]
},
{
"id": "AL",
"name": "Alagoas",
"city": [
"Agua Branca",
"Anadia",
"Vicosa"
]
}
]
}
JS:
app.controller('infoController',['$scope','$http',function($scope,$http){
$scope.getLocation = function(val) {
return $http.get('/data1.json', {
params: {
address: val,
sensor: false
}
}).then(function(response){
return response.data.estados.map(function(item){
return item.cidades.toString();
});
});
};
标记:
<div class="input-group ">
<input type="text" ng-model="customerCity" placeholder="Type something" uib-typeahead="address for address in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-no-results="noResults" class="form-control">
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
<div ng-show="noResults">
<i class="glyphicon glyphicon-remove"></i> No Results found
</div>
</div>