简体   繁体   English

AngularJS - 将多个get请求收集到json数组中,然后传递给指令

[英]AngularJS - Collecting multiple get requests into json array and then passing to directive

I am new to angular and been struggling how to solve my problem. 我是棱角分明的新人,一直在努力解决我的问题。

I need to access API multiple times for users data, store everything as JSON array and when all the data is collected(all results as one array) it needs to be passed to directive which will use it to draw visualization(eg. d3.js-pie chart). 我需要多次访问API以获取用户数据,将所有数据存储为JSON数组,并且当收集所有数据时(所有结果都作为一个数组),需要将其传递给指令,该指令将使用它来绘制可视化(例如.d3.js) -饼形图)。

$scope.allData = [];

$http.get("****link here****/students")
    .success(function (data) {
        students = data;

        for (i = 0; i < students.length; i = i + 1) {

            $http.get("**** link here ****/quest/" + students[i].id)
                .success(function (data) {
                    quest = data;

         $scope.allData.push({
                            id: quest.id,
                            value: quest.length
                        });
           }
        }

and then pass it to directive as 然后将其传递给指令as

   <bar-chart data='allData'></bar-chart>

even if I set watch in directive and have scope as '=' the directive gets empty array. 即使我在指令中设置监视并且范围为'=',指令也会变为空数组。

In my other code when I just do one http get call for json array I can pass it to directive easily and it works fine. 在我的其他代码中,当我只对json数组执行一次http get调用时,我可以轻松地将它传递给指令并且它工作正常。


EDIT1: EDIT1:

OK so I use premises now, but still allData array is 0. Even with simple example like this: 好的,所以我现在使用前提,但仍然allData数组为0.即使这样的简单示例:

 $scope.allData = [];
 var promieses = [];
 for (i = 0; i < 10; i = i + 1) {

            promieses.push($http.get("***link***/student/" + students[i].id));
}

$q.all(promieses).then(function (data) {
    for (i = 0; i < data.length; i = i + 1) {
        $scope.allData.push("test");
    }
});

in html {{ allData ]] // 0 在html {{allData]] // 0

This is a great place to unleash the power of $q . 这是释放$q力量的好地方。 You can wait for all the promises to resolve and then process them using $q.all method. 您可以等待所有承诺解析,然后使用$q.all方法处理它们。 It simply Combines multiple promises into a single promise that is resolved when all of the input promises are resolved. 它简单地Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.

See this example: 看这个例子:

students = data;
var promises = [];
for (i = 0; i < students.length; i = i + 1) {

    promises.push($http.get("**** link here ****/quest/" + students[i].id));

}

$q.all(promises).then(function(response) {
    for (var i = 0; i < response.length; i++) {
         $scope.allData.push({
             id: response[i].data.id,
             value: response[i].data.length
         });
    }
})

See it in action here: http://plnkr.co/edit/TF2pAnIkWquX1Y4aHExG?p=preview 请点击此处查看: http//plnkr.co/edit/TF2pAnIkWquX1Y4aHExG?p = preview

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

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