2015-02-09 73 views
0

我试图循环一个变量,搜索ID的,然后进行ajax调用以获取成功函数中不同ID的Detailcontent我尝试循环接收内容并发送电子邮件。ajax调用中的嵌套循环/ angular

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

你的专业人员可能会帮助我解决这个问题。

期待您的帮助!

这里是我的角度应用程序文件的特定部分:

//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

你有2个解决方案

使用承诺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循环基于乌尔列表
  • 上声明一个范围变量保存在函数内部空数组的数组
  • 推响应对象中

避免定义http.get内部$导致意外行为的for循环

+1

谢谢。伙计们。所以我推荐去承诺。从未使用过。我会进入这篇文章http://andyshora.com/promises-angularjs-explained-as-cartoon.html。嗯,这个分离功能的解决方案可以工作。我会尝试...... – elpeyotl 2015-02-09 13:19:02

+0

如果那篇文章满足你的情况,那么通过你从这篇文章得出的结论回答你的问题,接受你的答案来帮助别人。 – 2015-02-09 13:20:49