我将数据从对象传递到$resource
。但是,在将对象发送到资源之前,我将其发送出去以便处理到geocode
它。
地理编码功能会改变 $scope.formData
,以便向其添加lat
和lng
数据。
我把这些都搞定了。但是,当我创建$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;
}])
任何人都可以解释这个吗?