我有以下代码,但是我想在Angular'$ scope中使用PlaceService(map).search(request)
返回的数据。变量。
目前我有回调函数为结果数据分配一个新数组,但Angular $ scope变量无法访问它,因为Google Places是异步的。
我可以看到两种方法,但我不太确定如何执行它们。 从我所看到的,我可以:
1)在没有回调的情况下访问findPlaces()函数中的搜索结果
或
2)将$ scope变量传递给回调函数'storeResults'
function findPlaces(addrLocation, $scope) {
// Geocoder already started
// prepare variables (filter)
var type = myType;
var radius = document.getElementById('radius').value;
// prepare request to Places
var request = {
location: addrLocation,
radius: radius,
types: [type]
};
// send request
service = new google.maps.places.PlacesService(map);
service.search(request, storeResults);
}
function storeResults(results, status) {
//Ensure something is found
if (status == google.maps.places.PlacesServiceStatus.OK) {
//First we clear the list of previously found places
clearResults();
for (var i = 0; i <= results.length-1; i++) {
setCREsults(results[i]);
//alert(searchResults[i].name + ' ' + searchResults[i].address);
}
}
else if (status == google.maps.places.PlacesServiceStatus.ZERO_RESULTS) {
alert('Sorry, nothing is found');
}
}
请告诉我如何在Angular应用中使用搜索结果。