Angularjs 如何根据从模型中提取的最大值验证表单输入?

Angularjs 如何根据从模型中提取的最大值验证表单输入?,angularjs,Angularjs,我有以下表格: <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="ite

我有以下表格:

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

输入type=“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>

您超过了最大值
无论如何,在plnkr中输入大于最大值的数字之前,您不会看到验证


示例-示例编号输入指令生成
角度.module('numberExample',[])
.controller('ExampleController',['$scope',function$scope){
$scope.example={
价值:12
};
}]);
数
必修的!
无效号码!
这是一个大数字!
请尝试输入一个大于10的数字
value={{example.value}}
myForm.input.max={{myForm.input.$max}}
myForm.input.$valid={{myForm.input.$valid}}
myForm.input.$error={{{myForm.input.$error}}
myForm.$valid={{myForm.$valid}}}
myForm.$error.required={{{!!myForm.$error.required}}}

正如我看到的,您的输入没有任何验证。我读到的是在输入中添加max=位。我把它放在item.qty的输入中,这是我想要根据max(item.maxqty)进行验证的内容。我该怎么做呢?您可以手动(在键盘上)输入一个高于最大值的数字。初始数字无论如何都高于最大值。非常感谢。但是,我的实现仍然不起作用。我认为对input name属性的动态调用导致了这个问题。有什么想法吗?对输入的动态调用??我不明白。你是说检索到了de ng模型??是的。我发现您无法将输入名称绑定到模型。我必须找到一个解决方法。当昨天看你的代码时,我觉得与item.id的用法不一致。我认为这可能会导致form.item.id出现问题
    <!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>