简体   繁体   中英

While loop is not working properly in angularjs

I am trying to use while loop in Angular. If I use alert in while loop, it give same quantity output. but I remove alert , the quantity is different. 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); instead of the alert. Browser may interrupt when you use alert because of the pop up.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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