我有这样的表格:
<div ng-repeat="item in ctrl.cartItems">
<div class="col-sm-4 cartItemLabel" ng-bind-html="item.label"> </div>
<div class="col-sm-2 inputItem">
<input class="form-control input" type="text" id="item.id" name="item.id" ng-change="ctrl.updateSub(item)" ng-model="item.qty" max="item.maxqty">
</div>
<div class="col-sm-1 inputItemValue">
<span ng-bind="item.maxqty"></span>
</div>
<div class="col-sm-2 inputItemValue">
<span ng-bind="item.value | currency:USD$:2"></span>
</div>
<div class="col-sm-1 inputItemValue">
$
</div>
<div class="col-sm-2 inputItem">
<input class="form-control" type="text" id="item.id" name="item.id" ng-model="item.subTotal">
</div>
<div class="col-sm-4"></div>
<div class="col-sm-2 error" ng-show="form.item.id.$invalid">
<small class="error" ng-show="form.item.id.$error.max">
You exceeded the maximum allowed
</small>
</div>
</div>
max的值设置为item.maxqty,它是从我的模型中的JSON中提取的。正确填充所有值。我似乎无法让验证工作。有任何想法吗?代码的重要部分深埋在灌木丛中:)所以,这是我需要验证的行:
<input class="form-control input" type="text" id="item.id" name="item.id" ng-change="ctrl.updateSub(item)" ng-model="item.qty" max="item.maxqty">
答案 0 :(得分:2)
输入类型中有一个最大值=“text”。如果它不是第一,它将无法工作。那是吗?
必须
<input class="form-control input" type="number" id="item.id" name="item.id" ng-change="ctrl.updateSub(item)" ng-model="item.qty" max="item.maxqty">
修改强> 此外,项目$ valid对此
是可以的<div class="col-sm-2" ng-hide="form.item.id.$valid" >
<small class="error" ng-show="form.item.id.$error.max">You exceeded the maximum </small>
</div>
无论如何,在模型更大之前你不会看到验证,我习惯(输入将不允许你输入一个大于最大值的数字)check this fiddle在plnkr中
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-number-input-directive-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0-rc.0/angular.min.js"></script>
</head>
<body ng-app="numberExample">
<script>
angular.module('numberExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.example = {
value: 12
};
}]);
</script>
<form name="myForm" ng-controller="ExampleController">
<label>Number
<input type="number" name="input" ng-model="example.value"
min="0" max="10" required>
</label>
<div role="alert">
<span class="error" ng-show="myForm.input.$error.required">
Required!</span>
<span class="error" ng-show="myForm.input.$error.number">
Not valid number!</span>
<span class="error" ng-show="myForm.input.$error.max">
THIS IS A BIG NUMBER!</span>
</div>
<h1> PLEASE TRY TO PUT A NUMBER BIGGER THAN 10</h1>
<tt>value = {{example.value}}</tt><br/>
<tt>myForm.input.max = {{myForm.input.$max}}</tt><br/>
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
</form>
</body>
</html>