gpt4 book ai didi

javascript - 在函数内使用 angularJS 验证输入

转载 作者:行者123 更新时间:2023-12-03 11:39:42 25 4
gpt4 key购买 nike

我的代码是这样的。

<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 我想在该函数执行之前验证我的文本框。文本框是纯数字字段并且是必需的。我想以 Angular 方式验证它。我怎样才能实现这个目标?

<强> Fiddle

最佳答案

<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/

关于javascript - 在函数内使用 angularJS 验证输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26335699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com