Angular.forEach仅影响最后一行

时间:2015-04-05 19:24:37

标签: angularjs

一整天都在试图说谎。希望一些新的眼睛可以得到它!

基本上我有一个范围:gridOrder,其中包含(ProdID:& Quantity :)。我使用产品ID从DB(数量和保留数量)获取数据。

然后我将它传递给一个计算某些数据的函数,并将所需的数据添加到另一个名为prodOrder []的作用域中。 prodOrder被赋予与等效gridOrder相同的索引(即如果gridOrder [2]然后创建了prodOrder [2])。

如果我按时间顺序工作,一切正常,如gridOrder [0],然后是gridOrder [1]但是如果我在gridOrder [x]并返回gridOrder [xa](a> 1)它将不会更新gridOrder [xa]上的值。

如果有人能提供任何见解,我们将不胜感激。

代码正在运行

 $scope.reservedStockFunction = function(){
        angular.forEach($scope.gridOrder, function(value, key){
            $scope.rvalue = value;
            $scope.rkey = key;
            console.log(value);console.log(key);
            //Check stock. 
            var reservedStockRequest = {prodID: value.prodID, prod_quantity: "", prod_reserved_quantity: "", table: "products"};
            //Send requst
            search_given_where.request(reservedStockRequest).success(function(data){
                updValues(data, key, value);
            });
        }); 
        function updValues(data, key, value){
            console.log(data);
            var i = key; 
            if (typeof $scope.prodOrder[i] == "undefined" || !($scope.prodOrder[i] instanceof Array)) {
                $scope.prodOrder[i] = {};
            }           
            //Set Quantity variable.
            if (value.quantity !== undefined){
                var qty = value.quantity;
            } else {
                var qty = 0;
            }
            //Set Product Quanity Variable.
            var prodQty = data[0].prod_quantity;
            //Set Reserved Product Quantity variable.
            var prodRsvdQty = data[0].prod_reserved_quantity;
            //Reserved quantity = current reserved quantity + quantity.
            prodRsvdQty = +prodRsvdQty + +qty;
            //If reserved quantity is greater than stock.
            if(prodRsvdQty > prodQty){
                var reservedStockMessage = "Product:" + value.prodID + "  Stock:" + prodQty + "  Reserved Stock:" + prodRsvdQty;
                //Let user know.
                $scope.reservedStockMessage[i] = {};
                $scope.reservedStockMessage[i].message = reservedStockMessage;
                //Show reserved stock options.
                $scope.reservedStock.show = true;
                //Create array to update product table.
                $scope.prodOrder[i].prodID= value.prodID;
                $scope.prodOrder[i].prod_reserved_quantity = prodRsvdQty;
                $scope.prodOrder[i].reserved = 1;
                //Set reserved on gridOrder array.
                value.reserved="1";
                data = null;
                key = null;
                value = null;
                qty = null;
                prodQty = null;
                prodRsvdQty = null;
            //If not.
            } else if (prodRsvdQty < prodQty){
                $scope.reservedStockMessage[i] = {};
                //Create array to update product table.
                $scope.prodOrder[i].prodID= value.prodID;
                $scope.prodOrder[i].prod_reserved_quantity = prodRsvdQty;
                $scope.prodOrder[i].reserved = 0;
                //Set Reserved on gridOrder array.
                value.reserved="0";
                data = null;
                key = null;
                value = null;
                qty = null;
                prodQty = null;
                prodRsvdQty = null;
            }
        };
    };

1 个答案:

答案 0 :(得分:1)

Josh,你使用了错误的回调updValues的参数。

你可以尝试使用:

search_given_where.request(reservedStockRequest).success(function(data){
    updValues(data, key, value);
});

使用键和值的范围值。