I'm trying to use the Typeahead directive to add an auto-completion field. If i write my code like the example at http://angular-ui.github.io/bootstrap/#/typeahead it works fine of course.

But i cannot get it to work when i try to encapsulated the $http call in a factory:

servicesModule.factory('LocationService', function($http) {
 return {
     getLocation : function (val) {
         $http.get('http://maps.googleapis.com/maps/api/geocode/json', {params: { address: val, sensor: false }});
     }
  }
});

And i call exactly the same as before :

$scope.getLocation = function(val) {
  return LocationService.getLocation(val).then(function(res){
      var addresses = [];
      angular.forEach(res.data.results, function(item){
        addresses.push(item.formatted_address);
      });
      return addresses;
    });
  };

While correctly injecting the service LocationService in my controller. I'm guessing it's something i'm missing with the promise or the callback?

Because the service does call my back office but in firebug i get "LocationService.getLocation(...) is undefined"

有帮助吗?

解决方案

Change the following in the factory

getLocation : function (val) {
     return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {params: { address: val, sensor: false }});
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top