所以我已经阅读了一些关于这个主题的帖子,但还没有完全弄清楚。我尝试实施在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>