javascript AngularJs:在数字输入字段中设置用于验证的条件最小值和最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29183718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:11:17  来源:igfitidea点击:

AngularJs : Set conditional min and max value for validation in number input field

javascriptangularjsangularjs-directive

提问by SagarVimal

I have created a web page which contains a form with two number input fields field1and field2. I'm doing the validation on these fields. field1has a minvalue 1 and maxvalue 100000 but field2should have minvalue is maxof field1or 1, anything which is greater and maxis 100000. I tried to assign the ng-modelof field1as minof field2but it not working.

我创建了一个网页,其中包含一个带有两个数字输入字段的表单field1field2. 我正在对这些字段进行验证。field1有一个min值 1 和max值 100000 但field2应该有min值是maxoffield1或 1,任何大于等于max100000 的值。我试图分配ng-modelof field1as minoffield2但它不起作用。

<div class="row">
  <div>
    <input ng-model="priceminrange" name="priceminrange" type="number"
    class="form-control" value="" placeholder="MinRange" min="1" max="100000">
    <span ng-show="form.priceminrange.$error.number">The value is not a valid number</span> 
    <span ng-show="form.priceminrange.$error.min || form.priceminrange.$error.max">
    The value must be in range 1 to 100000</span>
  </div>
  <div>
    <input ng-model="pricemaxrange" name="pricemaxrange" type="number"
    class="form-control" value="" placeholder="MaxRange" min={{priceminrange}} max="100000">
    <span ng-show="form.pricemaxrange.$error.number">The value is not a valid number</span> 
    <span ng-show="form.pricemaxrange.$error.min || form.pricemaxrange.$error.max">
    The value must be in range {{priceminrange}} to 100000</span>
    form.pricemaxrange.$error = {{form.pricemaxrange.$error}}
  </div>
</div>

采纳答案by Eder Ribeiro

Try this

试试这个

On controller create scope.

在控制器上创建范围。

$scope.priceminrange = 1;

回答by Michael Venable

You were close. The object priceminrangeis an Angular object that contains several methods and properties, all relating to the model. The property you want is called $modelValue, which contains the value entered by the user after it is parsed to its correct data type (number in this case).

你很接近。该对象priceminrange是一个 Angular 对象,它包含多个与模型相关的方法和属性。您需要的属性称为$modelValue,其中包含用户输入的值,该值被解析为正确的数据类型(在本例中为数字)。

Something like this should work, assuming you have wrapped the input elements in a form named "form."

假设您已经将输入元素包装在名为“form”的表单中,这样的事情应该可以工作。

<input ng-model="pricemaxrange" type="number" min="{{form.priceminrange.$modelValue}}">