angularjs 额外的表单验证规则(不是输入字段验证)

angularjs additional form validation rules (not input field validation)

向表单添加验证规则的angular方法是什么属于单个输入字段,而是多个字段值?

例如:

如果错误可以用 ng-messages 显示就更好了。我正在使用 angular 1.3.10.

没有内置功能,但需要很少的努力。

ng-messages 不依赖于任何特定的东西。它只需要一个对象,其键可以被 ng-message 引用。最简单的解决方案是连接到 submit 事件(无论如何你都可能这样做)和 运行 额外验证。

<form ng-submit="post()" name="myForm">
  <input type="checkbox" name="one" ng-model="one" />
  <input type="checkbox" name="two" ng-model="two" />
  <input type="submit" />
  <div ng-messages="formErrors">
    <p ng-message="tooMany">Please, check one checkbox only</p>
    <p ng-message="required">Please, check a checkbox</p>
  </div>
</form>

提交时调用函数 post() 将任何错误添加到对象 formErrors:

$scope.post = function() {
  ...
  var hasErrors = false;
  $scope.formErrors = {
  };

  if ($scope.one && $scope.two) {
    $scope.formErrors.tooMany = hasErrors = true;
  }

  if (!$scope.one && !$scope.two) {
    $scope.formErrors.required hasErrors = true;
  }

  if (hasErrors) {
     return;
  }
}