【发布时间】:2015-05-01 03:46:22
【问题描述】:
我正在使用 angular.js 制作动态表单。 输入字段
<div ng-show="condition()">
<input type="text" name="field" required>
</div>
如果 condition() 返回 false,则不会显示输入字段。 但是通过点击提交按钮,我会在 chrome 上显示消息:
An invalid form control with name='field' is not focusable.
嗯,一个解决方案是,使用 ng-required:
<div ng-show="condition()">
<input type="text" name="field" ng-required="condition()">
</div>
好吧,这里我要重复代码,多次使用condition()。
当你可以封装 ng-show 的时候,它变得很糟糕:
<div ng-show="condition1()">
<div ng-show="condition2()">
<input type="text" name="field"
ng-required="condition1() && condition2()">
</div>
</div>
有没有更好的方法,当输入可见时,所需的标签应该在那里,如果它是隐藏的,则不存在。
【问题讨论】: