我对 http://angular-ui.github.io/bootstrap/ 中提供的 angular-ui typeahead 示例做了一些小修改。
这是 Controller 中的原始代码。
$scope.getLocation = function(val) {
return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: val,
sensor: false
}
}).then(function(res){
var addresses = [];
angular.forEach(res.data.results, function(item){
addresses.push(item.formatted_address);
});
return addresses;
});
};
这是我所做的更改;
$scope.getLocation = function(val)
{
return $http.get('http://maps.googleapis.com/maps/api/geocode/json',
{
params:
{
address: val,
sensor: false
}
}).success(function(data, status, headers, config)
{
var addresses = [];
angular.forEach(data.results, function(item){
addresses.push(item.formatted_address);
});
return addresses;
});
};
我的代码有什么问题?我没有收到任何错误。 typeahead 只是不起作用。当我在输入框中输入文本时,我没有看到任何预先输入的文本。
谢谢。
最佳答案
.success()
方法实际上不返回typeahead
api 所期望的 promise 。 .then()
方法确实返回一个 promise 。参见 this answer有关 .success()
和 .then()
之间区别的更多详细信息。
关于javascript - 这个 Angular-ui typeahead 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23774344/