我想在Angular中使用while循環。 如果我在while循環中使用alert
,它會給出相同的數量輸出。但我刪除alert
,數量不同。那麼我的代碼有什麼問題。While循環在angularjs中無法正常工作
var quantity= $scope.quantity;
var i=0;
while(i< quantity)
{
order.createOrderLineItem(localStorage.getItem("orderID"), itemStr, undefined, $scope.errorCallback)
.success(function(data){
$scope.lineItemID = data.id;
alert("i:"+i);
var orderLineItemData = {};
if($scope.cusNote == "")
{
var existedNote = data.note || "";
orderLineItemData = {
"unitQty": $scope.quantity,
"note": existedNote,
};
//if adding taxRates 0, clover will returns error
if($scope.taxRates !== 0) {
orderLineItemData.taxRates = $scope.taxRates;
}
}
else
{
var customerNote = "";
if(data.note != undefined && data.note != null) customerNote = data.note;
customerNote = customerNote + "#order-request-[";
customerNote = customerNote + $scope.cusNote;
customerNote = customerNote + "]";
customerNote = customerNote.replace(/\\/g, '\\\\');
customerNote = customerNote.replace(/\"/g, '\\"');
console.log("customerNote:" + customerNote);
orderLineItemData = {
"unitQty":$scope.quantity,
"note": customerNote,
"taxRates": $scope.taxRates
}
}
order.updateOrderLineItem(localStorage.getItem("orderID"), $scope.lineItemID, JSON.stringify(orderLineItemData), undefined, $scope.errorCallback)
.success(function(data){
if($scope.modCount == 0)
{
location.href = "/" + $routeParams.slug;
}
$scope.addModifierItem($scope.lineItemID);
});
});
i++;
}
那麼我該如何糾正這段代碼呢?
似乎在同步循環內使用異步調用...結果是意外的。 – Kinnza 2014-11-24 10:40:57
那麼,我該如何在其中進行更改 – 2014-11-24 10:46:34
從代碼示例中不清楚,但它看起來好像要以串行方式運行所有請求,而不是並行運行。是這樣嗎? – 2014-11-24 11:34:50