我想在HTML中添加一些验证(我正在使用Angular 7),因此我尝试将验证表单包装在div
的周围。现在我发现,我无法将表单验证与ngModel
结合使用,因此我需要进行验证或ngModel
。
我有一个p-calender
元素,我不想在这里进行验证。所以我有以下代码:
<div class="p-col-1"></div>
<div class="p-col-3">
<label>Date:</label> <br>
<p-calendar [(ngModel)]="startDate" dateFormat="dd.mm.yy" class="medium-field"></p-calendar>
</div>
<div class="p-col-8"></div>
并出现以下错误:
ERROR Error:
ngModel cannot be used to register form controls with a parent formGroup directive. Try using
formGroup's partner directive "formControlName" instead. Example:
<div [formGroup]="myGroup">
<input formControlName="firstName">
</div>
In your class:
this.myGroup = new FormGroup({
firstName: new FormControl()
});
Or, if you'd like to avoid registering this form control, indicate that it's standalone in ngModelOptions:
Example:
<div [formGroup]="myGroup">
<input formControlName="firstName">
<input [(ngModel)]="showMoreControls" [ngModelOptions]="{standalone: true}">
</div>
现在该错误表明,如果我想保留ngModelOptions
,则需要添加ngModel
,因此我将代码修改为:
<p-calendar [(ngModel)]="date" dateFormat="dd.mm.yy" class="medium-field" [ngModelOptions]="{standalone: true}"></p-calendar>
不幸的是,此行的错误仍然存在。我错过了什么吗?