Javascript Angular$资源数据可供查看,但不在代码中
我是个棱角分明的傻瓜,对一个特殊的问题感到非常沮丧 我有一个$resource从服务器返回数据,其中包含键/值对,即detail.name、detail.email等 我可以使用{detail.name}}符号在视图上访问这些信息,但我无法在代码中访问这些信息,这让我发疯,因为我需要这些数据来处理这些事情 如何在后端访问它 以下是生成数据的代码:Javascript Angular$资源数据可供查看,但不在代码中,javascript,php,html,angularjs,google-maps,Javascript,Php,Html,Angularjs,Google Maps,我是个棱角分明的傻瓜,对一个特殊的问题感到非常沮丧 我有一个$resource从服务器返回数据,其中包含键/值对,即detail.name、detail.email等 我可以使用{detail.name}}符号在视图上访问这些信息,但我无法在代码中访问这些信息,这让我发疯,因为我需要这些数据来处理这些事情 如何在后端访问它 以下是生成数据的代码: mydata = Appointment.get({id: $stateParams.id}, function(data){ geocode
mydata = Appointment.get({id: $stateParams.id}, function(data){
geocoder.geocode(data);
$scope.detail = data;
});
我认为:
<address class="text-left">
{{detail.address_1}}</br>
{{detail.city}}</br>
{{detail.postcode}}</br>
</address>
</hr>
<p> {{detail.lat}}</p>
<p> {{detail.lng}}</p>
<p> {{center}}</p>
如果有人感兴趣,请联系地理编码器工厂:
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 = 'AIzaSyACVwB4i_6ujTrdjTMI-_tnsDrf6yOfssw';
$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;
}])
有人能帮忙吗?您可以利用$resource对象的承诺返回,然后在controller中利用该承诺 工厂 控制器
geocoder工厂的设置完全错误……应该返回$http承诺并使用承诺解析,然后设置数据。真的需要理解异步意味着什么,并深入研究我在上一篇文章中提供的链接question@Mobaz这对你有帮助吗?
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 = 'AIzaSyACVwB4i_6ujTrdjTMI-_tnsDrf6yOfssw';
$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;
}])
angular.module('MyApp')
.factory('Appointment', function($resource){
return $resource('/api/admin/:id', { id: "@_id" }, {
query: {method:'GET', isArray:true},
update: { method:'PUT' }
});
})
mydata = Appointment.get({id: $stateParams.id}).$promise;
mydata.then(function(data){
geocoder.geocode(data);
$scope.detail = data;
});