在函数内使用angularJS验证输入

时间:2014-10-13 08:27:12

标签: javascript angularjs

我的代码是这样的。

<form name="palletForm" novalidate=novalidate>
<div ng-app='myApp' ng-controller="MainCtrl">
    <!--Small Package starts here -->
    <div ng-repeat="prdElement in packageElement track by $index" class="package-grid">
        <table class="hovertable">
            <thead>
                <tr>
                    <th>Line Quantity#</th>
                    <th>Ship Quantity</th>
                    <th>PickQuantity</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.LINQTY}}</td>
                    <td>{{data.SHPQTY}}</td>
                    <td>{{data.PickQty}}</td>
                    <td>
                        <input ng-model="data.newquantity" placeholder="Quantity" required=required type="number" />
                    </td>
                    <td>{{data.SHPQTY}}</td>
                </tr>
                <tr>
                    <td width="100%" colspan="4">
                        <button ng-show="prdElement.show" type="button" ng-click="newPackageItem( prdElement,$event)">Finish Package</button>
                    </td>

                </tr>
            </tbody>
        </table>
    </div>
    <!--Small Package ends here -->
</div>

    angular.module('myApp', ['ui.bootstrap']);
angular.module('myApp', []).controller('MainCtrl', ['$http', '$scope', function ($http, $scope) {

    var counter = 0;

    $scope.packageElement = [{

        show: true,
        palletClosed: false,
        disableNextPallet: false,
        Data: [{
            "ITMLN": 100,
            "ITCLS": "EPZ",
            "ITEMNO": "021041029300",
            "LINQTY": 1,
            "SHPQTY": 0,
            "PickQty": 1000,
            "Qtyplt": 0,
            "packed": 0

        }, {
            "ITMLN": 100,
            "ITCLS": "EPZ",
            "ITEMNO": "4901000002201",
            "LINQTY": 1,
            "SHPQTY": 0,
            "PickQty": 2000,
            "Qtyplt": 0,
            "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.PickQty != row.newquantity || row.PickQty != 0) {
                    row.PickQty = row.PickQty - row.newquantity;
                    row.SHPQTY = Number(row.SHPQTY) + Number(row.newquantity);
                }

            });

            npackageElement.show = true;
            angular.forEach(packageElement.Data, function (row) {

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

            });
            $scope.packageElement.push(npackageElement);

        };

}]);

在内部按钮单击我正在调用函数newPackageItem我想在该函数执行之前验证我的文本框。文本框是仅限数字字段且必填。我想以有角度的方式验证它。我怎样才能做到这一点?

Fiddle

2 个答案:

答案 0 :(得分:1)

<body ng-app="phonecatApp">
    <form ng-controller="PhoneListCtrl" name="myForm">
        <p>
            <input name="Quantity" ng-model="data.newquantity" placeholder="Quantity" required=required type="number" />
<span class="error" ng-show="myForm.Quantity.$error.pattern">
</span>

        </p>
    </form>
</body>

并在您的javascript文件中

var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function ($scope) {
    $scope.pattern = /^[0-9]*$/;
});

这是我上传到jsfiddle的验证示例:

http://jsfiddle.net/sfk1bu1y/1/

答案 1 :(得分:0)

AngularJS表单验证是您的朋友:

https://docs.angularjs.org/guide/forms