如何使必填字段的 ngMessage 仅在脏或提交表单时显示? [英] How to make ngMessage for required fields only show when dirty or submitting a form?

查看:24
本文介绍了如何使必填字段的 ngMessage 仅在脏或提交表单时显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 AngularJS 1.3.0 RC0 和 angular-messages.ng-messages 在表单最初加载并保持原始状态时尽职尽责地显示那些必填"字段的错误消息.所以新加载的表单页面充满了错误消息.这不是我想要的.如何使 ngMessage 的必填字段仅在这些字段脏或提交表单时显示?

我已经阅读了官方文档,没有任何线索.并试图将ng-show =fieldName.$dirty""放在ng-messages div中,没有用.任何帮助将不胜感激!

解决方案

使用 ng-if 属性检查具有 ng-messages 的标签上的 $dirty.

示例:

<div ng-message='required'>必填字段</div>

I'm using AngularJS 1.3.0 RC0 and angular-messages. ng-messages dutifully shows error messages for those "required" fields when a form is initially loaded and pristine. So the newly loaded form page is filled up with error messages. This is not what I want. How to make ngMessage for required fields only show when those fields are dirty or submitting a form?

I have read the official doc, no clue. And tried to put "ng-show = "fieldName.$dirty"" in the ng-messages div, did not work. Any help would be highly appreciated!

解决方案

Use the ng-if attribute to check for $dirty on the tag that has ng-messages.

Example :

<div ng-messages='myForm.myControl.$error' ng-if='myForm.myControl.$dirty'>
    <div ng-message='required'>Required field</div>
</div>

这篇关于如何使必填字段的 ngMessage 仅在脏或提交表单时显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆