[英]Angular 4 : ngNativeValidate is not allowed in Form
I'm using Angular 4 to build a form and required attribute is not working. 我正在使用Angular 4构建表单,并且必填属性不起作用。 I tried adding
<form ngNativeValidate>...</form>
as suggested by many answers but i got : 我尝试根据许多答案的建议添加
<form ngNativeValidate>...</form>
,但我得到了:
Attribute ngNativeValidate is not allowed here.
此处不允许使用ngNativeValidate属性。
Here is my code : 这是我的代码:
<form #f="ngForm" (ngSubmit)="submit()" ngNativeValidate>
<div class="form-group input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input class="form-control" type="text" [(ngModel)] = "email" name='email' placeholder="Email" required/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-def btn-block">Submit</button>
</div>
</form>
try to replace this : <input class="form-control" type="text" [(ngModel)] = "email" name='email' placeholder="Email" required/>
尝试替换为:
<input class="form-control" type="text" [(ngModel)] = "email" name='email' placeholder="Email" required/>
with this : <input class="form-control" type="text" ngModel #email="ngModel" name='email' placeholder="Email" required/>
使用以下命令:
<input class="form-control" type="text" ngModel #email="ngModel" name='email' placeholder="Email" required/>
so all your code should look like this : 因此您的所有代码应如下所示:
<form #f="ngForm" (ngSubmit)="submit()"> <div class="form-group input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> <input class="form-control" type="text" ngModel #email="ngModel" name='email' placeholder="Email" required/> </div> <div class="alert alert-danger" role="alert" *ngIf="!email.valid && email.touched" > <span *ngIf="!email.valid"> This field is required</span> </div> <div class="form-group"> <button type="submit" class="btn btn-def btn-block">Submit</button> </div> </form>
Also remove the unnecessary ngNativeValidate and it should work now. 还要删除不必要的ngNativeValidate,它现在应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.