简体   繁体   English

在angularjs中同步多个$ http响应

[英]Synchronize multiple $http response in angularjs

Simply i need call an API to get response of user addresses and corresponding to each address i have to call google API to get the longitude and latitude. 我只需要调用一个API来获取用户地址的响应,并对应于每个地址,我就必须调用google API来获取经度和纬度。
I have tried too much and about all methods but at last my i am still looking to my empty hands. 我已经尝试了太多并且几乎所有方法,但是最后我仍然希望空着手。
please help me figure out where i am wrong. 请帮助我找出我错了。

app.controller('myController', function($scope, $rootScope, $http,$q){

$scope.myGeoKey="AIzaSyCqoOf_Kx5wzrYh3ioxFQ_xyz";

// FIRST API REQUEST
var url="http://xyz/abc?origin=noida";
$http.get(url, {
        headers:{'Content-Type': 'application/json'}
}).
then(function(data, status, headers, config) {

    // response is like
    /*
       [
            {
                "address":"Sector 64, Noida",
                "state": "Uttar Pradesh"
            },
            {
                "address":"Sector 62, Noida",
                "state": "Uttar Pradesh"
            },
            {
                "address":"CP, New Delhi",
                "state": "Delhi"
            }
        ]
    */

    var synchronizeData = [];
    var asynchronizeData = [];

    for (var key in data.data.response){

        var rawAddress=data.data.response[key].address+", "+data.data.response[key].state;
        var geoUrl="?address="+rawAddress+"&key="+$scope.myGeoKey;

        // GET LATITUDE AND LONGITUDE FOR EACH API RESPONSE

        $http.get("https://maps.googleapis.com/maps/api/geocode/json"+geoUrl).
        then(function(data, status, headers, config) {

            if(data.data.status=="OK") {
                var geometry=data.data.results[0].geometry.location;

                var latlng={
                    "lat": geometry.lat,
                    "long": geometry.lng
                }
                asynchronizeData.push(latlng);
            }

        });
    }

    // console.log(asynchronizeData) // WORKING TILL HERE

    $q.all(asynchronizeData).then(function(response){

        // console.log(response) // THIS IS NOT WORKING

        for (var i=0,len = response.length;i<len;++i){
            synchronizeData.push(response[i]);
        }
        $scope.synchronized = synchronizeData;

        console.log($scope.synchronized); // FINAL OUTPUT NOT WORKING

    });
});
});

Please Please.. 请请..
Thanks 谢谢

.all() need array of promises as param (or hash of promises). .all()需要将promise数组作为参数(或promise的哈希值)。 And you pass an array of literal objects! 然后,您传递了一系列文字对象!

Read the doc about $q 阅读有关$ q的文档

I am really not sure about that but, try to push all your second http call (the looped one) in your asyncArray. 我真的不确定,但是,请尝试将所有第二个HTTP调用(循环的一个)推送到asyncArray中。 Then tell us what is happen in your .all() function. 然后告诉我们您的.all()函数中发生了什么。

Because each $http Will return a promise, so you stack promises in your array, so you can .all(). 因为每个$ http将返回一个Promise,所以您可以在数组中堆叠Promise,因此可以使用.all()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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