[英]AngularJS access JSON Object returned from $http.get
I try to access to json object generated by google api. 我尝试访问由Google API生成的json对象。
function getAvengers() {
return $http.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA')
.then(getAvengersComplete)
.catch(getAvengersFailed);
function getAvengersComplete(response) {
return response.data;
}
function getAvengersFailed(error) {
console.log('XHR Failed for getAvengers.' + error.data);
}
}
TestCtrl.dataTest = dataservice.getAvengers();
console.log(TestCtrl.dataTest.status);
Log generate undefined. 日志生成未定义。 Could you help me?
你可以帮帮我吗?
Thanks 谢谢
As getAvengers
returns with a promise, you cannot use it's result as an immediate value, but you can subscribe to it's resolution. 当
getAvengers
返回承诺时,您不能将其结果用作立即值,但可以订阅其分辨率。 See a promise tutorial for more details. 有关更多详细信息,请参见Promise教程 。
function getAvengers() {
return $http.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA')
.then(getAvengersComplete)
.catch(getAvengersFailed);
function getAvengersComplete(response) {
return response.data;
}
function getAvengersFailed(error) {
console.log('XHR Failed for getAvengers.' + error.data);
}
}
TestCtrl.dataTest = null;
dataservice.getAvengers().then(function(data) {
TestCtrl.dataTest = data;
console.log(TestCtrl.dataTest.status);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.