foreach内scope对象中的AngularJS更新值
我需要在表单提交上重新计算scope对象内的一些值 所以我有:foreach内scope对象中的AngularJS更新值,angularjs,scope,Angularjs,Scope,我需要在表单提交上重新计算scope对象内的一些值 所以我有: $scope.submit = function() { if ($scope.searchPlace) { var address = $scope.searchPlace + ', ' $scope.land; geocodeAddress(address, function(latLng){ angular.forEach($scope.places,functi
$scope.submit = function() {
if ($scope.searchPlace) {
var address = $scope.searchPlace + ', ' $scope.land;
geocodeAddress(address, function(latLng){
angular.forEach($scope.places,function(value,key){
var distance = getDistance(value.latitude,value.longitude,latLng.lat(),latLng.lng())
value.distance = distance;
});
});
}
};
$scope.places类似于[{'name':'place_a','distance':0},{'name':'place_b','distance':0}]
但我看不到前端中scope对象的任何更改。我错过了什么
下面是上面使用的两个函数
var getDistance = function(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
var geocodeAddress = function(address, callback) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0].geometry.location);
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
};
如果
getDistance
函数返回一个承诺,您应该这样使用它:
getDistance(value.latitude,value.longitude,latLng.lat(),latLng.lng()).then(function(response){
var distance = response.data;
value.distance = distance;
});
什么是
geocodeAddress()
?一个进行计算的函数。它工作正常并返回一些值。距离是一个数值。如果我将结果记录到控制台,则一切正常。唯一的问题是,我没有看到范围更新发布其代码。这很重要。如果getdistance函数有任何$http调用,那么如果您不返回承诺,则该值将不会更新。我已将这两个函数添加到问题中没有承诺。。。我必须添加它吗?很简单calculation@Sobakinet最好将getDistance的代码添加到您的问题中