简体   繁体   English

While循环在angularjs中无法正常工作

[英]While loop is not working properly in angularjs

I am trying to use while loop in Angular. 我试图在Angular中使用while循环。 If I use alert in while loop, it give same quantity output. 如果我在while循环中使用alert ,它会输出相同的数量。 but I remove alert , the quantity is different. 但是我删除了alert ,数量有所不同。 So what is wrong in my code. 那么我的代码出了什么问题。

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++;
        }

So how can I correct this code? 那么如何纠正此代码?

I am not sure but , alert may be the reason of that. 我不确定,但是 ,警报可能是其原因。 Can you try to use console.log("i:"+i); 您可以尝试使用console.log("i:"+i);console.log("i:"+i); instead of the alert. 而不是警报。 Browser may interrupt when you use alert because of the pop up. 当您使用警报时,浏览器可能会由于弹出而中断。

--Edited-- --Edited--

this is the service that you will need to implement into your application. 这是您需要在应用程序中实现的服务。 You will need to notify this service at your success and it will trigger any observe that is implemented after the notify 您将需要在成功时通知此服务,它将触发在通知后实施的任何观察

 app.service('checkService',['$q',function($q){
        var checkRequest = $q.defer();

        this.notifyRequestIsDone= function(){
            checkRequest .notify();
        };

        this.observeRequestStatus = function(){
            return checkRequest .promise;
        };
    }]);

Here is observe example of the code above add this to your controller where you need to increase your value after the request is done 这是上面代码的观察示例,将其添加到控制器中,在完成请求后,您需要在其中增加价值

checkService.observeRequestStatus ().then(null,null,function(){
    //... Your code
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM