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
AngularJs : Set conditional min and max value for validation in number input field
提问by SagarVimal
I have created a web page which contains a form with two number input fields field1
and field2
. I'm doing the validation on these fields. field1
has a min
value 1 and max
value 100000 but field2
should have min
value is max
of field1
or 1, anything which is greater and max
is 100000. I tried to assign the ng-model
of field1
as min
of field2
but it not working.
我创建了一个网页,其中包含一个带有两个数字输入字段的表单field1
和field2
. 我正在对这些字段进行验证。field1
有一个min
值 1 和max
值 100000 但field2
应该有min
值是max
offield1
或 1,任何大于等于max
100000 的值。我试图分配ng-model
of field1
as min
offield2
但它不起作用。
<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 priceminrange
is 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}}">