我正在尝试在 Angular 中使用 while 循环。
如果我在 while 循环中使用 alert
,它会给出相同数量的输出。但我删除alert
,数量不同。那么我的代码有什么问题。
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++;
}
那么我该如何更正此代码?
最佳答案
我不确定,但是,警报可能是原因。您可以尝试使用 console.log("i:"+i);
而不是警报。当您使用警报时,浏览器可能会因弹出窗口而中断。
--已编辑--
这是您需要在应用程序中实现的服务。您需要在成功后通知此服务,它将触发通知后实现的任何观察
app.service('checkService',['$q',function($q){
var checkRequest = $q.defer();
this.notifyRequestIsDone= function(){
checkRequest .notify();
};
this.observeRequestStatus = function(){
return checkRequest .promise;
};
}]);
这是上面代码的观察示例,将其添加到您的 Controller ,您需要在请求完成后增加您的值
checkService.observeRequestStatus ().then(null,null,function(){
//... Your code
});
关于javascript - While 循环在 angularjs 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27102386/