无法在angularJS中进行数学计算

时间:2014-10-10 10:38:28

标签: javascript angularjs

我想要实现的是这样的。将有两列,其中包含我手中当前数量的产品,下一个是打包列,它将具有默认值为0的文本框,当我在该文本框中更改值并按下提交按钮时我想要该值从我的第一列(数量)中减去(已完成)。在此之后我想在最后一栏中将文本框中的值加起来然后继续,这样我就可以看到到目前为止已经包装了多少项目 到目前为止,我已经做了类似的事情

<div ng-app='myApp' ng-controller="MainCtrl">
<div ng-repeat="prdElement in packageElement track by $index" class="package-grid">
    <table class="hovertable">
        <thead>
            <tr>
                <th>Line #</th>
                <th>Quantity in Plt</th>
                <th>Allready Packed</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="data in prdElement.Data" ng-init="data.newquantity  = 0">
                <td>{{data.itemId}}</td>
                <td>
                    <input type="text" ng-model="data.newquantity" placeholder="Quantity" required=required />
                </td>
                <td>{{data.packed}}</td>
            </tr>
            <tr>
                <td width="100%" colspan="4">
                    <button ng-show="prdElement.show" ng-click="newPackageItem( prdElement,$event)">Next Pallet</button>
                </td>
            </tr>
        </tbody>
    </table>
</div>

    (function () {
    angular.module('myApp', []).controller('MainCtrl', function ($scope) {
        var counter = 0;
        $scope.packageElement = [{
            name: counter,
            show: true,
            Data: [{
                name: 'item 1',
                itemId: '284307',
                quantity: '100',
                packed: 0

            }, {
                name: 'item 2',
                itemId: '284308',
                quantity: '200',
                packed: 0
            }]
        }];

        $scope.newPackageItem = function (packageElement, $event) {

            var npackageElement = {};
            angular.copy(packageElement, npackageElement);
            counter++;
            packageElement.show = false;

            npackageElement.name = counter;
            angular.forEach(npackageElement.Data, function (row) {
                if (row.quantity != row.newquantity || row.quantity != 0) {
                    row.quantity = row.quantity - row.newquantity;
                    row.packed = row.newquantity;
                }

            });

            npackageElement.show = true;
            angular.forEach(packageElement.Data, function (row) {
                row.packed = row.packed + row.newquantity;
            });
            $scope.packageElement.push(npackageElement);

        };
    });

}());

除非添加Quantity in Plt并按下按钮,否则我希望该数量显示在最后一列(已打包)并跟进后续行。我已经尝试了,你可以在这里看到

 row.packed = row.packed + row.newquantity;

但是添加不能正常工作,而是值连接。 Fiddle

2 个答案:

答案 0 :(得分:1)

尝试

row.packed = Number(row.packed) + Number(row.newquantity);

以下是fiddle

答案 1 :(得分:1)

尝试

row.packed = parseInt(row.packed, 10) + parseInt(row.newquantity, 10);