我曾与一家工厂集成谷歌地图自动完成和承诺,我希望你服务。
您只需要在出厂前用$ http incuida替换此请求的autocompleteService。app.factory('Autocomplete', function($q, $http) {
和$ http请求var deferred = $q.defer();
$http.get('urlExample').success(function(data, status, headers, config) {
deferred.resolve(data);}).error(function(data, status, headers, config) {
deferred.reject(status);});
return deferred.promise;
#Description
{{$index}}{{direction.description}}var app = angular.module('myApp', []);
app.factory('Autocomplete', function($q) {
var get = function(search) {
var deferred = $q.defer();
var autocompleteService = new google.maps.places.AutocompleteService();
autocompleteService.getPlacePredictions({
input: search,
types: ['geocode'],
componentRestrictions: {
country: 'ES'
}
}, function(predictions, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
deferred.resolve(predictions);
} else {
deferred.reject(status);
}
});
return deferred.promise;};return {
get: get};});app.controller('myController', function($scope, Autocomplete) {$scope.$watch('search', function(newValue, oldValue) {
var promesa = Autocomplete.get(newValue);
promesa.then(function(value) {
$scope.directions = value;
}, function(reason) {
$scope.error = reason;
});
});});
问题本身就是:deferred.resolve(varResult);
当你做得很好并且请求时:deferred.reject(error);
当出现错误时,然后:return deferred.promise;