Angular 1.5.7中动态添加和删除的表单字段的角度形式验证

时间:2016-07-18 17:12:31

标签: javascript angularjs validation angularjs-forms

所以我已经阅读了一些关于这个主题的帖子,并且还没有想到它。我试图实现在https://scotch.io/tutorials/building-dynamic-angular-forms-with-ngrepeat-and-ngform找到的解决方案,但没有成功。验证会将ng-invalid类添加到元素中,但不会使用ng-show显示错误消息。这是我到目前为止所做的事情。

首先,这里是一个plnkr:http://plnkr.co/edit/0Xh0o82N1xQc7Qi5Mhfe?p=preview

<h1>Dynamic Field</h1>
<div ng-controller="EmailCtrl">
  <hr>
  <a ng-click="addEmail()" href="#">Add Email</a>
  <hr>
  <form name="email_form">
    <label>Main Form
    <input type="email" ng-model="test.email" name="email" />
    <div class="help-block" ng-show="email_form.email.$touched">
    <span ng-show="email_form.email.$error.email">ERROR</span>
  </div>
  <p></p>
  <div ng-repeat="email in emails">
    <ng-form="sub_form">
      sub form <input name="sub_email" type="email" ng-model="email.email" />
      <div class="help-block" ng-show="sub_form.sub_email.$touched">
        <span ng-show="sub_form.sub_email.$error.email">ERROR</span>
      </div>
      <a ng-click="removeEmail($index)" href="#">Remove Email</a>
      </ng-form>
  </div>
  </label>
</form>

1 个答案:

答案 0 :(得分:1)

<ng-form="sub_form>
</ng-form>

应该是:

<ng-form name="sub_form">
</ng-form>

希望它有所帮助!