简体   繁体   English

如何组合ng-message消息

[英]How to combine ng-message messages

I am new to AngularJS but I have searched extensively and could not find a working answer to this question, maybe its just not possible the way I have it in mind. 我是AngularJS的新手,但是我已经进行了广泛的搜索,但是找不到这个问题的可行答案,也许这就是我想到的方式。

What I would like is to be able to combine error conditions so that I can use more generic error messages in the ng-messages module. 我想要的是能够组合错误条件,以便我可以在ng-messages模块中使用更通用的错误消息。 This saves us a lot of time maintaining texts as our application is multi-lingual. 由于我们的应用程序是多语言的,因此这节省了我们维护文本的大量时间。 In my example it would be great to combine minlength, maxlength, and pattern and have it reference 1 generic message. 在我的例子中,将minlength,maxlength和pattern组合起来并将其引用为1通用消息会很棒。 The only way I have gotten it to work is for a separate ng-message for each type and then reuse the error text which seems redundant to me. 我让它工作的唯一方法是为每种类型单独的ng-message ,然后重用错误文本,这对我来说似乎是多余的。 Hopefully it's something short I am missing like not understanding when/how to use , or ||. 希望它是一些简短的东西我不想理解何时/如何使用,或||。

<form id="myFormId" name="myForm" novalidate>
  <input name="sText" ng-model="someText" 
  type="text"
  required
  ng-minlength="8" minlength="8"
  ng-maxlength="8" maxlength="8" 
  ng-pattern="/^[a-zA-Z0-9]{8,8}$/" pattern="/^[a-zA-Z0-9]{8,8}$/">

<div ng-messages="myForm.sText.$error" role="alert">
  Error message:
    <div ng-message="required">Required text missing</div>
    <div ng-message="minlength || maxlength || pattern">Not right length or bad pattern - Why does this not work? I have also tried using comma , instead of || </div>
    <div ng-message="minlength">Too short - this does work but does not change even if this is removed</div>
</div>
</form>

I have created this simple Plunk to illustrate what I am trying to do: 我创建了这个简单的Plunk来说明我想要做的事情:

EDIT 1 编辑1

I do realize I could use a single regex pattern expression but the above validations is strictly to reproduce the issue and show an example. 我确实意识到我可以使用单个正则表达式模式表达式,但上述验证仅用于重现问题并显示示例。 I have other validations I would like to combine that could not be expressed with a single pattern. 我还有其他一些我想要组合的验证,无法用单一模式表达。

ng-messages will show error message inside ng-messages directive element, but that has limitation that you could only display single error ng-message inside the ng-messages div. ng-messages将在ng-messages指令元素中显示错误消息,但这有限制,您只能在ng-messages div中显示单个错误ng-message

So if you wanted to show multiple ng-message inside ng-messages directive you need to add ng-messages-multiple attribute on ng-messages directive element. 因此,如果要在ng-messages指令中显示多个ng-message ,则需要在ng-messages指令元素上添加ng-messages-multiple属性。

Docs Link 文档链接

Markup 标记

<div ng-messages="myForm.sText.$error" role="alert" ng-messages-multiple>
    Error message:
    <div ng-message="required">
        Required text missing
    </div>
    <div ng-message="minlength, maxlength, pattern">
        Not right length or bad pattern - Why does this not work? I have also tried using comma , instead of ||(OR)
    </div>
    <div ng-message="minlength">
        Too short - this does work but does not change even if this is removed
    </div>
</div>

Working Plunkr 工作Plunkr

Update 更新

After angular document updation I came to know that ng-messages doesn't support to show multiple ng-message error inside ng-messages , for solving this problem we should have ng-messages-multiple attribute on ng-messages element. 角文件更新用后,我才知道, ng-messages不支持显示多ng-message中的错误ng-messages ,为解决这个问题,我们应该有ng-messages-multiple的属性ng-messages元素。

From Docs 来自Docs

By default, ngMessages will only display one error at a time. 默认情况下, ngMessages只显示一个错误。 However, if you wish to display all messages then the ng-messages-multiple attribute flag can be used on the element containing the ngMessages directive to make this happen. 但是,如果要显示所有消息,则可以在包含ngMessages指令的元素上使用ng-messages-multiple属性标志来实现此目的。

Markup 标记

<div ng-messages="myForm.sText.$error" role="alert" ng-messages-multiple>
    Error message:
    <div ng-message="required">
        Required text missing
    </div>
    <div ng-message="minlength, maxlength, pattern">
        Not right length or bad pattern - Why does this not work? I have also tried using comma , instead of ||(OR)
    </div>
    <div ng-message="minlength">
        Too short - this does work but does not change even if this is removed
    </div>
</div>

Working Plunkr 工作Plunkr

In Angular 1.4 you can specify multiple errors for a ng-message: 在Angular 1.4中,您可以为ng-message指定多个错误:

<div ng-message="minlength, maxlength">
  Your email must be between 5 and 100 characters long
</div>

See documentation 文档

Inorder to make your ng-message more generic you can keep all your error messages at one place and use it when required. 为了使您的ng-message更通用,您可以将所有错误消息保存在一个位置,并在需要时使用它。 You could do this using ng-message-include. 您可以使用ng-message-include执行此操作。

Have a look at : Reusing and Overriding Error Messages 看看: 重用和覆盖错误消息

http://www.yearofmoo.com/2014/05/how-to-use-ngmessages-in-angularjs.html#reusing-and-overriding-error-messages . http://www.yearofmoo.com/2014/05/how-to-use-ngmessages-in-angularjs.html#reusing-and-overriding-error-messages

I think you will like to implement this. 我想你想实现这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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