我正在尝试向Angular Bootstrap Typeahead传递一个承诺,但我总是遇到以下错误: TypeError:无法读取未定义的属性“length”
厂:
angular.module('app').factory('geoCoding', ['$http', 'geoUtils', function ($http, geoUtils) {
var request= function(location){
return $http.get('http://open.mapquestapi.com/geocoding/v1/address',{params:{location:location}});
};
var ret = {};
ret.locate = function (location) {
return request(location).then(function (data) {
if (data && data.data && data.data.results && data.data.results[0]) {
var locations = [];
data.data.results[0].locations.forEach(function (location) {
locations.push({name: geoUtils.location2String(location), location: location});
});
return locations;
}
});
};
return ret;
}]);
控制器:
$scope.getLocations = function(){
console.log('scope',$scope.inputLocation);
return geoCoding.locate($scope.inputLocation);
}
模板:
<input type="text" class="form-control oo-focus" placeholder="Search" typeahead="location as location.name for location in getLocations()" typeahead-wait-ms="500" ng-model="inputLocation" />
使用旧版Angular Bootstrap-UI 0.5.0一切正常。问题出在Bootstrap-UI 0.6.0(bootstrap3_bis2)上。 https://github.com/angular-ui/bootstrap/tree/bootstrap3_bis2
答案 0 :(得分:2)
如果您正在使用AngularJS 1.2.0-rc.2,则承诺存在问题(更改了承诺自我引用的公开 - 或者像这样的想法:))。我无法告诉你为什么,但你可以在这里找到更多信息:
Typehead broken when using promises in Angularjs 1.2.0rc2
对我来说,wirking解决方案是设置promise。$$ v变量。所以在你的例子中,代码应该是:
$scope.getLocations = function(){
console.log('scope',$scope.inputLocation);
var promise = geoCoding.locate($scope.inputLocation);
promise.$$v = promise;
return promise;
}
我同意作者(wilsonjackson),这很hacky!无论如何希望它会帮助你。
答案 1 :(得分:2)
Angular 1.2.0-rc.3已经发布,适用于该版本:http://code.angularjs.org/1.2.0-rc.3/