AngularJS在foreach内部的范围对象中更新值



我需要重新计算表单上的范围对象中的某些值。

所以我有:

 $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':'plot_a','anding':0},{'name'':'plot_b','dange':0}]

,但我看不到前端范围对象的任何更改。我想念什么?

这是上面使用的两个功能

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

最新更新