angular - Error : ngModel cannot be used to register form controls with a parent formGroup directive
问题描述
This is the parent component. Added a child component and passed the form. Here Using both the form Control Name & NgModel(Is this a best practice). While loading the page console error " ngModel cannot be used to register form controls with a parent formGroup directive."
<div class="inner-wrapper">
<form [formGroup]="form">
<pat-demo-information [group]="form"></pat-demo-information>
<div class="panel-body">
<div class="row">
<div class="col-lg-2 col-md-3 col-sm-3">
<div class="ui-input-group">
<input type="text" maxlength="15" class="form-control" [(ngModel)]="notif.en.No" formControlName="epiNo">
<span class="input-bar"></span>
<label>No.</label>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
This is the child component
@Component({
selector: 'patient-demographic-information',
templateUrl: './pat-demo-information'
})
export class PatDemoInfoComponent implements OnInit {
@Input() patientForm: FormGroup
noti: MaltreatmentNoti;
instid: number;
instPatientid: number;
latitude: number;
longitude: number;
enMpi: Mpi = new Mpi();
constructor(private formBuilder: FormBuilder) {
}
ngOnInit() {
this.notification = new MaltreatmentNoti();
this.notification.enMpi = new Mpi();
yhis.getFromGroup();
}
private getFromGroup() {
this.patientForm = this.formBuilder.group({
'instPatientid': new FormControl('', Validators.compose([Validators.required])),
})
}
}
//child html
<div class="panel panel-default" [formGroup]="patientForm">
<div class="panel-heading">Patient Information</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="ui-input-group">
<ng-select #reportInstitute [items]="selectInst" id="reportingInstitute"
[virtualScroll]="true" placeholder="Select" bindLabel="label" bindValue="value"
[(ngModel)]="instid" formControlName="patientInstId" required>
<ng-template ng-option-tmp let-item="item" let-index="index">{{item.label}}</ng-template>
</ng-select>
<span class="input-bar"></span>
<label>Institution<span class="mdtr">*</span></label>
<span *ngIf="patientForm.controls['patientInstId'].hasError('required') && (isError || patientForm.controls['patientInstId'].touched)" class="tooltiptext">{{'Institution is required'}}</span>
</div>
</div>
</div>
</div>
</div>
Here using reactive forms for validation & for other wise use template driven, to get & set values.
while loading the page, an error in console.
RROR 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>
What would be the reason for this error ? what is the solution ? Thanks in advance.
解决方案
同时使用表单控件名称和 NgModel(这是最佳实践)
不,这不是一个好习惯。
如果我们使用FormGroup
,则不要使用 [(ngModel)]
.
不过,如果您想同时使用两者,则如错误本身所述,请使用ngModelOptions
<input [(ngModel)]="showMoreControls" [ngModelOptions]="{standalone: true}">
推荐阅读
- node.js - 为什么我的选择语句在工作更新语句之后返回旧数据?
- c# - 使用 ajax jquery 将部分视图加载到特定页面的最佳方法是什么?
- javascript - 覆盖JS中现有键的值
- r - 如何在 R 中获得印度城市的纬度?
- angular - Angular:如何管理无效的网址
- ios - 如何获取捕获的最新视频?
- google-cloud-platform - 我们可以使用其凭据获取 Google Cloud Console 项目的名称或详细信息吗?
- angular - 我的项目使用 Angular CLI 1.7.4 安装正确的 Angular Material 版本是什么?
- php - 复选框未在使用 PHP 按下按钮时更新 SQL 查询
- java - 在 Spring 5.1.0 中加载和切换属性源最优雅的方式是什么?