离子上的GPS:无法设置未定义的属性

时间:2016-01-18 13:27:36

标签: angularjs ionic-framework

我以这种方式返回GPS应用程序的地址

 geocoder.geocode(request, function(data, status) {
    if (status = google.maps.GeocoderStatus.OK) {
      if (data[0] != null) {
        alert("address is: " + data[0].formatted_address);
        $scope.$apply(function() {  
        $scope.addresses = data[0].formatted_address;
        //$scope.apply();
        })
      } else {
        alert("No address available");
      }
    }
  })

我以这种方式获取范围变量:

<p>address: {{addresses}}</p>

当我部署应用程序时,我收到此错误: 未捕获的TypeError:无法设置属性&#39;地址&#39;未定义的 那可能是错的。

3 个答案:

答案 0 :(得分:0)

删除传递给地理编码回调函数的$ scope变量。您正在覆盖控制器的$ scope变量。

确保控制器声明注入$ scope变量

app.controller('MyCtrl', function($scope) {
   // do your thing
});

答案 1 :(得分:0)

看起来控制器中依赖注入的params列表中缺少'$ scope'。

angular.module('MyGeoCoderControllerModule', [])
.controller('GeoCoderController', ['$scope', function($scope) {


 }
]);

答案 2 :(得分:0)

$scope.addresses = "";
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(your_lat, your_lng);
var request = {
    latLng : latlng    
 };
 geocoder.geocode(request, function(data, status){
      if(status = google.maps.GeocoderStatus.OK){
          if(data[0] != null){                      
              $scope.$apply(function(){    
                   $scope.addresses = data[0].formatted_address;
              });
           } else {
                  alert("No address available");
           }
      }
 })