我想我知道问题所在。 承诺进入后台,循环继续进行,但是我如何才能使此代码起作用?
对于每个vm.applicantsDetails
,我需要检查customerNumbers var relatedCustomerNumbers = vm.applicantsDetails[i].RelatedCustomerNumbers
的数组,对于每个relatedCustomerNumbers,我都需要发出REST请求
for (var i = 0; i < vm.applicantsDetails.length; i++) {
var relatedCustomerNumbers = vm.applicantsDetails[i].RelatedCustomerNumbers;
for (var j = 0; j < relatedCustomerNumbers.length; j++) {
// get the customer number
var customerNumber = relatedCustomerNumbers[j];
// To here everything is fine
service.getCustomerData(customerNumber).then(function(response) {
// This prints always the vm.applicantsDetails.length - 1!!!
console.log(i);
// i is always 1 , which is the vm.applicantsDetails.length - 1
var applicantType = null;
switch (i) {
case 0:
applicantType = "primary";
break;
case 1:
applicantType = "secondary";
break;
}
console.log(applicantType); // Always secondary
}
}
}