3

我正在尝试使用 Typeahead 指令来添加自动完成字段。如果我像http://angular-ui.github.io/bootstrap/#/typeahead中的示例那样编写代码, 它当然可以正常工作。

但是当我尝试将 $http 调用封装在工厂中时,我无法让它工作:

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

我打电话和以前完全一样:

$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;
    });
  };

在我的控制器中正确注入服务 LocationService 时。我猜这是我在承诺或回调中缺少的东西?

因为该服务确实打电话给我的后台但在萤火虫中我得到“LocationService.getLocation(...)未定义”

4

1 回答 1

3

在工厂更改以下内容

getLocation : function (val) {
     return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {params: { address: val, sensor: false }});
 }
于 2014-03-11T10:35:13.097 回答