Angular:表单验证和自定义指令

时间:2016-12-29 22:33:03

标签: javascript angularjs

我有两个输入,我需要使用以下限制验证每个字段:值B 必须始终小于值A ,否则显示错误消息。这是example

我知道我需要创建一个指令,但我甚至不知道如何开始。

HTML

<form name="exampleForm">
    <label>Value A</label>
    <input type="number" name="valueA" data-ng-model="valueA" required="required"/>
    <div ng-messages="exampleForm.valueA.$error">
        <div ng-message="required">This field is required</div>
    </div>
    <p>
    <label>Value B</label>
    <input type="number" name="valueB" data-ng-model="valueB" required="required"/>
    <div ng-messages="exampleForm.valueB.$error">
        <div ng-message="required">This field is required</div>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

<input type="number" name="valueB" data-ng-model="valueB" max="{{ valueA - 1 }}" required="required"/>
  <div ng-messages="exampleForm.valueB.$error">
    <div ng-message="required">This field is required</div>
    <div ng-message="max">This field is too large</div>
  </div>