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

标签 javascript angularjs

我正在尝试在 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/

相关文章:

javascript - createReadStream 对大文件不起作用/非常慢

Angularjs 将 Controller 解析为字符串

javascript - AngularJS 错误 : $interpolate:interr Interpolation Error

java - Spring Boot可执行jar无法访问索引页

javascript - 消息在控制台中安慰两次 - ReactJS

javascript - KendoUI Treeview 子项显示为未定义

javascript - 隐藏在 Angular Bootstrap Accordion 中的动态内容

unit-testing - ngAnimate 单元测试不添加类

javascript - Android 后台服务与 React Native

javascript - 我是否需要将 'return' 语句放在 'casper.then' block 中,以便父函数等待子函数完成执行?