Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何组合ng消息_Javascript_Angularjs_Error Handling_Ng Messages - Fatal编程技术网

Javascript 如何组合ng消息

Javascript 如何组合ng消息,javascript,angularjs,error-handling,ng-messages,Javascript,Angularjs,Error Handling,Ng Messages,我是AngularJS的新手,但我已经搜索了很多次,找不到这个问题的有效答案,也许只是我脑子里想的不可能 我希望能够组合错误条件,以便在模块中使用更通用的错误消息。由于我们的应用程序是多语言的,这为我们节省了大量维护文本的时间。在我的示例中,最好将minlength、maxlength和pattern结合起来,并让它引用1条通用消息。我让它工作的唯一方法是为每种类型分别创建一条ng消息,然后重用对我来说似乎多余的错误文本。希望这是我缺少的一些东西,比如不知道什么时候/如何使用,或者| | <

我是AngularJS的新手,但我已经搜索了很多次,找不到这个问题的有效答案,也许只是我脑子里想的不可能

我希望能够组合错误条件,以便在模块中使用更通用的错误消息。由于我们的应用程序是多语言的,这为我们节省了大量维护文本的时间。在我的示例中,最好将minlength、maxlength和pattern结合起来,并让它引用1条通用消息。我让它工作的唯一方法是为每种类型分别创建一条ng消息,然后重用对我来说似乎多余的错误文本。希望这是我缺少的一些东西,比如不知道什么时候/如何使用,或者| |

<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>

错误消息:
缺少必需的文本
长度不正确或图案不好-为什么不起作用?我也试过用逗号代替| |
太短-此选项确实有效,但即使将其删除,也不会改变
我创建了以下内容来说明我正在尝试做的事情:

编辑1


我确实意识到我可以使用一个单一的正则表达式,但是上面的验证严格地再现了这个问题并给出了一个示例。我还想将其他无法用单一模式表示的验证组合起来。

为了使ng消息更通用,您可以将所有错误消息保存在一个位置,并在需要时使用它。您可以使用ng消息include执行此操作

查看:重用和覆盖错误消息


我想您会希望实现这一点。

ng messages
将在
ng messages
指令元素中显示错误消息,但这有一个限制,即您只能在
ng messages
div中显示单个错误
ng messages

因此,如果您想在
ng messages
指令内显示多条
ng messages
,则需要在
ng messages
指令元素上添加
ng messages multiple
属性

标记

<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>
<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>

在Angular 1.4中,您可以为ng消息指定多个错误:

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

您的电子邮件长度必须介于5到100个字符之间

请参见

谢谢您的回复。我以前看过这个网站,它确实对如何构造消息有很好的了解,但是在这种情况下,它不是跨模型或控制器模型重用消息的问题。我想使用通用但特定于模型的消息,这些消息可以在多种情况下显示。这正是我所担心的。我在Angular网站上看到了一个使用逗号的演示,所以我想我可以在这里使用多个属性,但它们可能完全是其他意思?我尝试ng消息的原因是,我可以保证在链中始终只显示一条错误消息,而不是多条消息。这不再是准确的信息。参见John Knoop的回答。@Will no man..OP要求他在
ng messages
指令中显示多条ng messages..&当时我回答了问题
ng messages multiple
doc中没有的东西。。您也可以验证更新答案和plunkr。看起来您的更新答案确实回答了原始问题。谢谢,我将在下周回来工作时尝试。