简体   繁体   中英

Can I validate input with dynamically assigned min attribute in AngularJS?

I've got two input fields of type number. I want second to have minimum attribute dependant on first field. I can dynamically change the attribute, but ng-valid does not change, when value is lower than minimum.

jsfiddle code example: http://jsfiddle.net/NBhn4/87/

Is that a bug or am I doing something wrong? I found this: https://github.com/angular/angular.js/issues/2404 . It seems to suggest, that the problem was fixed. In my jsfiddle example I am using angular 1.3.5 and it's still not working.

<input type="number" ng-model="max" min={{min}} name='max'>

Using this, changes the minimum, but ng-valid seems not be be triggered after change.

Its your angularjs version problem I hope. Use latest versions of angularjs and this code is working.

<form name="form" novalidate ng-init="min=1;number=0">
  Number:
  <input type="number" ng-model="number" name="number" min="{{min}}">
  <div style="color: red" ng-show="form.number.$error.min">Number must be at least {{min}}</div><br>

  Minimum:
  <input type="number" ng-model="min" name="min">

</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM