簡體   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