找到你要的答案

Q:Object passing data to an angular $resource missing some fields, but not others

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

I'm passing data into a $resource from an object. However, before sending the object to the resource I'm sending it away to be processed to geocode it.

The geocode function alters the $scope.formData to add lat and lng data to it.

I'm getting these all ok. However, when I create the $resource the lat and lng properties are missing, which is super odd.

Here is the code:

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

Appointment is defined as a factory:

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

})

created an external factory for the geocoder to be able to call it from any controller:

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;

}])

can anyone explain this?

我将数据从一个对象传递到一个$资源。然而,在发送对象,我送它去处理它的地理资源。

地理编码功能的改变scope.formdata美元增加LAT和液化天然气的数据,它。

我得到这些一切OK。然而,当我创造资源LAT和液化天然气的性质是缺失的,这是超级奇怪。

这里是代码:

$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;

}])

有人能解释一下吗?

javascript  angularjs  rest  object  angular-resource