Angular - ng-messages中的多个字段

时间:2016-04-02 16:55:12

标签: angularjs ng-messages

我正在使用ng-messages并想知道是否可以在一个代码块中检查多个字段而不必重复:

所以我有: (注意checkCustom是我写过的自定义指令)

<div ng-messages="registrationForm.field1.$error" ng-if="registrationFormValid">
    <span ng-message="required">please enter field 1</span>
    <span ng-message="checkCustom">please check custom value</span>
</div>

<div ng-messages="registrationForm.field2.$error" ng-if="registrationFormValid">
    <span ng-message="required">please enter field 1</span>
    <span ng-message="checkCustom">please check custom value</span>
</div>

但是我更喜欢这样的东西:

<div ng-messages="registrationForm.field1.$error || registrationForm.field2.$error" ng-if="registrationFormValid">
    <span ng-message="required">please enter field 1</span>
    <span ng-message="checkCustom">please check custom value</span>
</div>

这样的事情可能吗?

由于

3 个答案:

答案 0 :(得分:4)

A form's controller有一个名为$error的便捷属性。这是一个对象哈希,包含对具有失败验证器的控件或表单的引用,其中键是错误名称(例如emailrequiredminlength,等)它们的值是控件或表单的数组,它们具有给定错误名称的失败验证器 - source

这意味着我们只需检查此$error对象是否包含特定的密钥名称,就可以全局检查表单中的错误

看一下这个例子:

angular.module('myApp', ['ngMessages']);
[ng-cloak] {
  display: none !important;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">

<div ng-app="myApp" ng-cloak>
  <form name="myForm">
    <div class="form-group">
      <h3>Validation messages:</h3>

      <div ng-messages="myForm.$error" multiple>
        <div ng-message="required" class="alert alert-danger">There's at least one <strong>required</strong> field</div>
        <div ng-message="minlength" class="alert alert-danger">There's at least one field which doesn't fulfill the <strong>minlength</strong> condition</div>
        <div ng-message="email" class="alert alert-danger">The form contains an invalid <strong>email</strong> input</div>
      </div>
    </div>
    <div class="form-group">
      <label>Required Input 1</label>
      <input type="text" ng-model="myModel1" required>
    </div>
    <div class="form-group">
      <label>Required Input 2</label>
      <input type="text" ng-model="myModel2" required>
    </div>
    <div class="form-group">
      <label>Required Input 3 which has min-length="3"</label>
      <input type="text" ng-model="myModel3" required ng-minlength="3">
    </div>
    <div class="form-group">
      <label>Required Input 4 which is type="email"</label>
      <input type="email" ng-model="myModel4" required>
    </div>
  </form>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular-messages.min.js"></script>

答案 1 :(得分:0)

如果您不想在所有块中添加相同的消息并将其添加到一个位置,您可以执行以下操作:

  1. 在文件中添加所有消息

    <!-- remote file: error-messages.html --><div ng-message="required">please enter field 1</div> <div ng-message="checkCustom">please check custom value</div>

  2. 只需将该文件包含在您想要的位置即可。

    <div ng-messages="registrationForm.field1.$error" ng-messages-include="error-messages.html"> </div>

  3. 因此代码被重用。对于不同的错误消息,覆盖块内的消息。

    有关更多信息,请查看这个惊人的blog post。 我不认为你可以在一个ng-messages块中添加两个字段。

答案 2 :(得分:0)

只需使用

<div ng-if="registrationForm.field1.$error.required || registrationForm.field2.$error.required">
<span class="required">please enter field 1</span>
</div>