angular ngMessages自定义验证ng-message

时间:2016-08-18 10:23:56

标签: angularjs ng-messages

我有这个自定义验证

function uiEmailEquals() {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, ngModel) {
      scope.$watch(attrs.uiEmailEquals, function() {
        ngModel.$validate();
      });
      ngModel.$validators.isEquals = function(value) {
        var email = scope.$eval(attrs.uiEmailEquals);
        if (!email || !value) {
          return false;
        }
        return (value === email);
      };
    }
  };
}

<input type="email" tabindex="4" name="cemail" class="form-control" ng-model="vm.data.cemail" placeholder="confirm email" ng-required="true" ui-email-equals="vm.data.email"
/>
<div class="help-block" ng-messages="form.cemail.$error" ng-if="form.cemail.$touched">
    <p class="error-message" ng-message="required">Required</p>
    <p class="error-message" ng-message="email">Not valid email</p>
    <p class="error-message" ng-message="uiemailequals">uiemailequals</p>
    <p class="error-message" ng-message="ui-email-equals">ui-email-equals</p>
    <p class="error-message" ng-message="uiEmailEquals">uiEmailEquals</p>
    <p class="error-message" ng-message="equals">equals</p>
</div>

问题是我找不到要设置的内容

NG-消息

请帮帮我吗?

2 个答案:

答案 0 :(得分:1)

方法名称为isEquals使用ng-message="isEquals"

答案 1 :(得分:1)

ngModel.$validators.isEquals - 这是您的自定义验证器的名称 - &gt; isEquals

<p class="error-message" ng-message="isEquals">is equals</p>