我一直在尝试查找有关如何使用角度验证来显示消息但具有特定 min 或 max 属性的信息。
如果我有一个具有类似orinput[type="number"]
属性的字段并且用户输入我想要一条验证消息,上面写着:。但是数字应该从属性中动态获取。min=1
max=10
11
"The number should be less than 10"
10
max
我想将消息放在单独的模板中。我有以下内容:
<!-- default ng-messages -->
<script type="text/ng-template" id="common-messages">
<span ng-message="required" class="help-block">This field is required</span>
<span ng-message="maxlength" class="help-block">Must be no longer than {{maxlength}} characters</span>
<span ng-message="min" class="help-block">This number must be greater than {{min}}.</span>
<span ng-message="max" class="help-block">This number must be less than {{max}}.</span>
<span ng-message="number" class="help-block">A number is required</span>
<span ng-message="date" class="help-block">A date is required</span>
</script>
然后以一种形式
<div class="container" ng-controller="app.packaging.PackagingAddController as package">
<form name="packageForm" ng-submit="package.save()" role="form" novalidate class="form-horizontal">
<input type="text" name="packageCount" ng-model="package.model.packageCount" class="form-control" max="10 " />
<div ng-messages="packageForm.packageCount.$error" role="alert" ng-show="packageForm.$submitted || packageForm.packageCount.$touched" ng-messages-multiple>
<div ng-messages-include="common-messages"></div>
</div>
</form>
</div>
问题是显示的错误是This number must be less than .
{{max}} 没有被注入。有什么方法可以实现这一点,以便我可以重用 common-messages 模板?
PS:这个线程How to get min and max values of input angularjs对我的问题没有用,因为它不重用模板,也不从html输入max属性中读取。