我有以下结构:
// JS
$scope.fields = [
{ text: 'Email', type: 'text', value: '' },
{ text: 'Password', type: 'password', value: '' }
]
// HTML
<div class="user">
<h2 class="header">Log in</h2>
<form name="login">
<input class="form-control" type="{{ field.type }}" placeholder="{{ field.text }}" ng-model="field.value" ng-repeat="field in fields" required />
<a class="btn primary" href="javascript:;" ng-click="login()">Log in</a>
</form>
</div>
我怎样才能$error.required
像这里显示的那样实现?http://plnkr.co/edit/VLOEfp?p=preview
我不知道在哪里放置:
<div class="" ng-show="myform.routingNumber.$error.required">
<span class="help-block">Please enter routing number</span>
</div>
也许我必须重新构造它ng-repeat
?
尝试以下类似的方法。你快到了。只是错过了一个基本点,所有控件都需要名称。Angular使用名称进行验证。
<div class="user">
<h2 class="header">Log in</h2>
<form name="login" novalidate="">
<div ng-repeat="field in fields" >
<input class="form-control" type="{{ field.type }}" placeholder="{{ field.text }}" ng-model="field.value" required name="{{field.name}}"/>
<div ng-show="login.{{field.name}}.$invalid">
<span class="help-block">Please enter</span>
</div>
</div>
<a class="btn primary" href="javascript:;" ng-click="login()">Log in</a>
</form>
</div>
因此,在您的控制器中添加名称,如下所示
$scope.fields = [
{ text: 'Email', type: 'text', value: '', name:'idname'},
{ text: 'Password', type: 'password', value: '', name:'password' }
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句