2017-07-26 89 views
1

我不理解为什么我不能从一个函数的值,并在另一个角度显示它。我的代码:结果范围函数angularjs

$scope.getLatitudeLongitude = function(address) { 
      var geocoder = new google.maps.Geocoder(); 
      $scope.latLng = []; 
      geocoder.geocode({ "address": address }, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK && results.length > 0) { 
        var location = results[0].geometry.location, 
         lat  = location.lat(), 
         lng  = location.lng(); 
        $scope.latLng.push(lat,lng); 
        return $scope.latLng; 
       } 

      }); 
}; 


$scope.save = function() { 

      $scope.address = $scope.getAddress(); 
      $scope.getLatitudeLongitude($scope.address).then(function(){ 
       console.log($scope.latLng); 
      }) 
} 

任何想法?

+0

$ scope.getLatitudeLongitude()应该返回一个承诺。检查它现在返回的内容。它返回一个数组。 – Vivz

+0

什么不工作?/ –

+0

确定。你没有承诺然后使用 –

回答

1

添加一个承诺getLatitudeLogitude像这应该工作。太控制器注入$q

$scope.getLatitudeLongitude = function(address) { 
       var deferred = $q.defer(); 
       var geocoder = new google.maps.Geocoder(); 
       $scope.latLng = []; 
       geocoder.geocode({ "address": address }, function(results, status) { 
        if (status == google.maps.GeocoderStatus.OK && results.length > 0) { 
         var location = results[0].geometry.location, 
          lat  = location.lat(), 
          lng  = location.lng(); 
         $scope.latLng.push(lat,lng); 
         deferred.resolve($scope.latLng); 
        } 
        else{ 
         // geocode error 
        deferred.reject(); 
        } 

       } 
       return deferred.promise; 

    }; 


$scope.save = function() { 

      $scope.address = $scope.getAddress(); 
      $scope.getLatitudeLongitude($scope.address) 
       .then(function(latLng){ 
        console.log(latLng); 
       },function(){ 
        //log your error here; 
       }; 
}; 
+0

它给了我错误说:无法读取属性“然后”未定义 – IleNea

+0

没有ü点击错误,并找出哪些线是错误 –

+0

论getLatitudeLongitude功能该行: (..)然后(函数()... – IleNea

0
$scope.getLatitudeLongitude = function(address) { 
       var geocoder = new google.maps.Geocoder(); 
       $scope.latLng = []; 
       geocoder.geocode({ "address": address }, function(results, status) { 
        if (status === google.maps.GeocoderStatus.OK && results.length > 0) { 
         var location = results[0].geometry.location, 
          lat  = location.lat(), 
          lng  = location.lng(); 
         $scope.latLng.push(lat,lng); 
         //return $scope.latLng; you don't need to return when the method execute the $scope will know and keep the value visible to $scope.save 
        } 

       }); 
    }; 

    $scope.getAddress = function(){ 
//perform your scope.getAddress process here and after you get your address do this 
$scope.addressRecieved = addressReturned; 
     }; 

    $scope.save = function() { 

       // $scope.address = $scope.getAddress(); 
$scope.address = $scope.addressRecieved; 
if($scope.address !== undefined) {    
    $scope.getLatitudeLongitude($scope.address); 
}else{ 
console.log('Address not defined!'); 
} 
     console.log($scope.latLng);   

    } 

//首先触发$ scope.getAddress,你甚至可以调用$ scope.getAddress方法里面的$ scope.save,以确保该地址总是被定义否则这个程序将无法按预期工作。 //你甚至可以使用回调函数e.g(){$ scope.getAddress,回调}