ajax调用/ angular中的嵌套循环

时间:2015-02-09 12:51:33

标签: javascript ajax angularjs

我正在尝试循环变量,搜索ID,然后进行ajax调用以获取Success函数中不同ID的Detailcontent我尝试循环接收的内容并将电子邮件发送出去。

它正在运行,但我在$ scope.subContactmail中收到了第一封电子邮件。我认为循环有问题,但我不明白。试图弄清楚整个晚上,不幸的是没有想法来到低谷。想法应该是,如果第一个循环完成,它将从第二个循环开始。但是目前第一个循环也是第二个循环。

Problaby你的专业人士可以帮助我解决这个问题。

期待您的帮助!

以下是我的角度应用文件的具体部分:

//find all contract relations id's from customer
      $scope.contactrelation = function (input) {
      $http.post('http://localhost/mamiexpress/mamiAPI/includes/php/searchContactsRelation.php', input).
          success(function(data, status, headers, config) {
          $scope.subContactDetails =  [];
          $scope.subContactmail =  [];
          $scope.subContactId = data;
          console.log($scope.subContactId);

              //GET ALL the subcontact ID's from the selected item
                var i=0;
                var subContactIdlenght = $scope.subContactId.length;
                while  (i < subContactIdlenght) {
                console.log($scope.subContactId[i].contact_sub_id);
                var number = $scope.subContactId[i].contact_sub_id;
                i = i + 1;

              //Send the ID to the API and get the user Details
                $http.post('http://localhost/mamiexpress/mamiAPI/includes/php/searchContactswithID.php', number).
                    success(function(data, status, headers, config) {
                        $scope.subContactDetails.push(data); // store it in subContactDetails
                        console.log($scope.subContactDetails);


                       //HERE COULD BE THE PROBLEM!!
                       // I want this loop to start when the first loop is finished but i have to run this in this success function.
                       // At the moment i get the first email twice!

                       //Loop trough ContactDetails and get the emails.   
                       if (i == subContactIdlenght){ 
                           var subContactDetailslength = $scope.subContactDetails.length;
                              for(var p=0; p < subContactDetailslength; p++) {
                              console.log($scope.subContactDetails[p].mail);
                              var number = $scope.subContactDetails[p].mail;
                              $scope.subContactmail.push(number);
                              };   
                        };

                    }).
                    error(function(data, status, headers, config) {
                    $scope.errormessage = data;
                    console.log(data);
                    });

                 };//ENDWHILE


        console.log(data);
        }).
        error(function(data, status, headers, config) {
         $scope.errormessage = data;
          console.log(data);
        });

2 个答案:

答案 0 :(得分:2)

你有2个解决方案

使用promise API(推荐):

类似的东西

var wheatherPromise = $http.get(...);
var timePromise = $http.get(...);

var combinedPromise = $q.all({
    wheather: wheatherPromise,
    time: timePromise
})

combinedPromise.then(function(responses) {
    console.log('the wheather is ', responses.wheather.data);
    console.log('the time is ', responses.time.data);
});

OR

只需执行以下操作:

  • 在分离的函数中生成ur $ http请求或(AJS服务是 推荐)。
  • 根据你的列表
  • 在for循环中调用该函数
  • 声明范围变量在函数
  • 中包含一个空数组
  • 推送数组中的响应对象

避免在for循环中定义$ http.get,导致意外行为

答案 1 :(得分:0)

我认为你需要的是promises/deferred API