javascript - ajax 调用/Angular 中的嵌套循环

标签 javascript ajax angularjs

我正在尝试循环一个变量,搜索 ID,然后进行 ajax 调用以获取成功函数中不同 ID 的详细内容我尝试循环接收的内容并获取电子邮件。

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

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

期待您的帮助!

这是我的 Angular 应用程序文件的特定部分:

//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个解决方案

使用 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);
});
<小时/>

或者

只需执行以下操作:

  • 在单独的函数中发出 $http 请求或(AJS 服务是 推荐)。
  • 根据您的列表在 for 循环中调用该函数
  • 声明作用域变量在函数内保存一个空数组
  • 将响应对象推送到数组中

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

关于javascript - ajax 调用/Angular 中的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410266/

相关文章:

javascript - AngularJS 阻止 $urlRouterProvider.otherwise 打开 "default"页面

javascript - Angular 自定义指令 : Calling a function with arguments from within the link function

javascript - 在 es6 中使用解构 switch 语句

javascript - 使用按钮传递变量(Ajax Modal)

javascript - jquery ajax发送数据并获取: SQLSTATE[HY093]: Invalid parameter number

javascript - 为什么 $scope 和 'this' 关键字在 Controller 内可以互换使用?

javascript - 适用于 HTML <p> 元素文本的 jQuery .change() 方法

javascript - 分页效果不好

javascript - Angular.js 和 Fabric.js : Fabric canvas changes behavior once code is moved to a Angular Directive

javascript - 使用 Nodejs 信息填充选择框