I try to play with bootstrap typeahead and angular. When current method was in controller - all worked fine:
$scope.getLocation_work = 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;
});
};
When I tried to put the logic into service, I started get errors:
ypeError: Cannot read property 'length' of undefined
in controller
$scope.getLocation = function(val) {
UtilsFactory.getLocation(val).then(function(res){
var addresses = [];
angular.forEach(res, function(item){
addresses.push(item.formatted_address);
});
return addresses;
});
};
Service
app.factory('UtilsFactory', ['$http', '$q', function($http, $q) {
return {
getLocation : function(val) {
var deferred = $q.defer();
var response = $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: val,
sensor: false
}
});
deferred.resolve(response);
return deferred.promise;
}
}
}
]);
Do I miss something?
Here is Demo in Plunker
Thanks,