Javascript 而循环在angularjs中工作不正常
我正在尝试使用while循环。 如果我在while循环中使用Javascript 而循环在angularjs中工作不正常,javascript,angularjs,Javascript,Angularjs,我正在尝试使用while循环。 如果我在while循环中使用alert,它将提供相同数量的输出。但是我删除了警报,数量不同。那么我的代码有什么问题呢 var quantity= $scope.quantity; var i=0; while(i< quantity) { order.createOrderLineItem( localStorage.getItem( "orderID" ), itemStr, undefine
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++;
}
var数量=$scope.quantity;
var i=0;
而(i<数量)
{
order.createOrderLineItem(localStorage.getItem(“orderID”)、itemStr、未定义的$scope.errorCallback)
.成功(功能(数据){
$scope.lineItemID=data.id;
警报(“i:+i”);
var orderLineItemData={};
如果($scope.cusNote==“”)
{
var existedNote=data.note | |“”;
orderLineItemData={
“单位数量”:$scope.quantity,
“注释”:现有注释,
};
//如果添加税率0,clover将返回错误
如果($scope.taxRates!==0){
orderLineItemData.taxRates=$scope.taxRates;
}
}
其他的
{
var customerNote=“”;
如果(data.note!=undefined&&data.note!=null)customerNote=data.note;
customerNote=customerNote+“#订单请求-[”;
customerNote=customerNote+$scope.cusNote;
customerNote=customerNote+“]”;
customerNote=customerNote.replace(/\\/g,\\\\\');
customerNote=customerNote.replace(/\“/g,\\\”);
日志(“customerNote:+customerNote”);
orderLineItemData={
“单位数量”:$scope.quantity,
“注意”:客户注意,
“税率”:$scope.taxRates
}
}
order.updateOrderLineItem(localStorage.getItem(“orderID”),$scope.lineItemID,JSON.stringify(orderLineItemData),未定义,$scope.errorCallback)
.成功(功能(数据){
如果($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;
};
}]);
下面是上面代码的示例,将其添加到您的控制器中,您需要在请求完成后增加您的值
checkService.observeRequestStatus ().then(null,null,function(){
//... Your code
});
似乎您正在同步循环中使用异步调用。。。结果出乎意料。因此,如何在itIt中进行更改代码示例并不清楚,但似乎您希望以串行方式而不是并行方式运行所有请求。是这样吗?我想是用户承诺的。这里有一个叫做async.each的东西,但它只针对nodejs。我想这里有一个建议:我看到您正在请求一个数据,并且正在为成功做一些工作。您应该注意到,若您正在调用http请求,则从服务器返回时会有延迟。因此,如果没有警报,javascript将不会等待返回一个增量“i”,但是如果添加警报,它将停止js,直到您点击弹出窗口上的按钮,这样就为请求返回留出了时间间隔。您可以广播或使用延迟来获得正确的成功。我希望这能帮助你在这方面如何使用defer