我試圖循環一個變量,搜索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);
});
謝謝。夥計們。所以我推薦去承諾。從未使用過。我會進入這篇文章http://andyshora.com/promises-angularjs-explained-as-cartoon.html。嗯,這個分離功能的解決方案可以工作。我會嘗試...... – elpeyotl 2015-02-09 13:19:02
如果那篇文章滿足你的情況,那麼通過你從這篇文章得出的結論回答你的問題,接受你的答案來幫助別人。 – 2015-02-09 13:20:49