显示基于多个条件的带有 ng 消息的验证消息
Display validation message with ng-messages based on multiple conditions
>我使用角度ng消息来显示验证消息
请考虑以下代码:
<ul ng-messages="formToValidate.fieldToValidate.$error" ng-messages-multiple>
<li ng-message="pattern">Invalid pattern</li>
<li ng-message="minlength, maxlength">Should contain no less than 7 and no more than 100 chars</li>
<li ng-message="!pattern && !minlength && !maxlength && onlyAfterAllOtherPassedValidation">
This validation message is shown only when all the other validations have passed and validation fails on onlyAfterAllOtherPassedValidation
</li>
</ul>
我想仅在所有其他验证都已通过并且验证仅在AfterAllOtherPassedValidation上失败时才显示最后一条验证消息。而且我不知道是否有可能将复杂条件传递给ng-message。只要我仍然可以使用 ng-messages
您可以在保存消息的元素上使用 ng-if,例如
<li ng-if="!formToValidate.fieldToValidate.$error.required && !formToValidate.fieldToValidate.$error.minLength && !formToValidate.fieldToValidate.$error.maxLength" ng-message="onlyAfterAllOtherPassedValidation">
This validation message is shown only when all the other validations have passed and validation fails on onlyAfterAllOtherPassedValidation
</li>
JonMac1374提出的解决方案当然有效。但最好一劳永逸地解决这个问题。我建议您注意指令使用表单错误。它有助于建立自己的支票。
jsfiddle上的活生生的例子。
<form name="ExampleForm">
<label>Password</label>
<input ng-model="password" name="password" minlength="2" pattern="^'d*$" required use-form-error="onlyAfterAllOtherPassedValidation" use-error-expression="!ExampleForm.password.$error.required && !ExampleForm.password.$error.minlength && !ExampleForm.password.$error.pattern"
/>
<pre>{{ExampleForm.password.$error|json}}</pre>
<div ng-messages="ExampleForm.password.$error" ng-messages-multiple="true" class="errors">
<div ng-message="required">
Your required is wrong
</div>
<div ng-message="pattern">
Your pattern is wrong
</div>
<div ng-message="minlength">
Your minlength is wrong
</div>
<div ng-message="onlyAfterAllOtherPassedValidation">
Your onlyAfterAllOtherPassedValidation
</div>
</div>
</form>
相关文章:
- AngularJs对ng消息的自定义替换
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- 如何将ng消息与<输入名称='用户[名称]'>
- ng消息包括不工作
- 从ng条消息中提取ng条消息密钥
- 为什么Angular ng消息'当=“0”时;“必需”'即使输入字段有输入,也会激发
- 为什么myForm$提交的不适用于ng消息,但适用于ng if
- 有角度的ng消息显示ng重复表单上的错误
- 正在使用ng消息验证动态表单
- 如果文本筛选器在ng重复中未返回结果,则显示消息
- AngularJS ng-click不显示ng-show消息
- 角度:当没有结果时显示一条消息 ng-重复角度 ?(具体情况)
- 角度验证消息不适用于 ng-show 和两个输入
- NG消息未显示
- 为列表项 ng 创建唯一的错误消息 - 重复角度
- 角度验证 NG 消息在提交和擦除文本后不显示必需
- 在错误消息中访问 ng 模式
- 显示基于多个条件的带有 ng 消息的验证消息
- 'ng消息'没有在angular js中实例化
- 对话框中未显示有角度的材质ng消息