将数据传递给角度$资源的对象缺少某些字段,而不是其他字段

时间:2015-05-04 14:58:15

标签: javascript angularjs rest object angular-resource

我将数据从对象传递到$resource。但是,在将对象发送到资源之前,我将其发送出去以便处理到geocode它。

地理编码功能会改变 $scope.formData ,以便向其添加latlng数据。

我把这些都搞定了。但是,当我创建$resource时,缺少lat和lng属性,这是超奇怪的。

以下是代码:

$scope.ok = function (){
    $scope.geocode($scope.formData);
    var data = $scope.formData;
    /* the lat and lng data are in the data object*/
    console.log(data);

        var appointment = new Appointment(data);
                    /* the lat and lng are missing from this resource object*/
                    console.log(appointment);
}

约会被定义为工厂:

angular.module('MyApp')
.factory('Appointment', function($resource){
    return $resource('/api/admin/:id', { id: "@_id" }, {
         query: {method:'GET', isArray:true}, 
         update: { method:'PUT' }
    });

})

为地理编码器创建了一个外部工厂,以便能够从任何控制器调用它:

angular.module('MyApp')
.factory('geocoder', ['$http','$state', function($http, $state){

var geocoder ={};

geocoder.geocode = function (formData){
    var myformData = {};
    var address = formData.address_1+', '+formData.city+', '+formData.postcode;

                var key = 'mykey';

                $http.get('https://maps.googleapis.com/maps/api/geocode/json?address='+address+'&key='+key).
                  success(function(results, status, headers, config) {
                    var results = results.results[0];
                    formData.lat = results.geometry.location.lat;
                    formData.lng = results.geometry.location.lng;
                        myformData = formData;
                    return myformData;          

                     // this callback will be called asynchronously
                    // when the response is available
                  }).
                  error(function(results, status, headers, config) {
                    // called asynchronously if an error occurs
                    // or server returns response with an error status.
                  });   
}


return geocoder;

}])

任何人都可以解释这个吗?

0 个答案:

没有答案