繁体   English   中英

从$ http.get返回的AngularJS访问JSON对象

[英]AngularJS access JSON Object returned from $http.get

我尝试访问由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);

日志生成未定义。 你可以帮帮我吗?

谢谢

getAvengers返回承诺时,您不能将其结果用作立即值,但可以订阅其分辨率。 有关更多详细信息,请参见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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM