weixin_33743661 2014-07-07 15:44 采纳率: 0%
浏览 49

Ajax循环调用

Here's a unique array of account id and I want to call Ajax to retrieve the relevant accounts.

var account_id_unique = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]; 

I decided to use a for loop and call AJAX inside the loop. However, instead of retrieving and storing different values to $scope.account = data;, I'm only storing the last value that I ran through the for loop. I tried to move $scope.account = data; to outside of the loop, but it's not possibly since I'm making Ajax call inside the loop. Is there a better way?

for (i=0; i<account_id_unique.length; i++){
    $http.get('api/accounts/'+ account_id_unique[i].toString())
        .success(function(data, status, headers, config){
         console.log(data);
         $scope.account = data;

    }).error(function(data, status, headers, config){
        console.warn(data);
    });

}; 

Additional: This is a snippet of html file. I'm trying to print out a list of accounts and artifacts to the front page.

SNS:{{ account.vendorCode }} identity id: {{account.identity.identityId}}
  • 写回答

1条回答 默认 最新

  • helloxielan 2014-07-07 15:54
    关注
    $scope.account = "";
    for (i=0; i<account_id_unique.length; i++){
        $http.get('api/accounts/'+ account_id_unique[i].toString())
            .success(function(data, status, headers, config){
             console.log(data);
             $scope.account = data;
    
        }).error(function(data, status, headers, config){
            console.warn(data);
        });
    
    }; 
    console.log("acccount is:" $scope.account);
    

    OR if you want to store multiple values,

    $scope.account = [];
    for (i=0; i<account_id_unique.length; i++){
        $http.get('api/accounts/'+ account_id_unique[i].toString())
            .success(function(data, status, headers, config){
             console.log(data);
             $scope.account[i] = data;
    
        }).error(function(data, status, headers, config){
            console.warn(data);
        });
    
    }; 
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的