问题描述
升级到 RC5 后,我们开始收到此错误:
ngModel 不能用于向父 formGroup 注册表单控件指示.尝试使用 formGroup 的合作伙伴指令formControlName";反而.例子:<div [formGroup]=myGroup"><input formControlName="firstName">
升级到 RC5 后,我们开始收到此错误:
ngModel 不能用于向父 formGroup 注册表单控件指示.尝试使用 formGroup 的合作伙伴指令formControlName";反而.例子:<div [formGroup]=myGroup"><input formControlName="firstName">
在你的课堂上:this.myGroup = new FormGroup({名字:new FormControl()});或者,如果您想避免注册此表单控件,表明它在 ngModelOptions 中是独立的:例子:<div [formGroup]=myGroup"><input formControlName="firstName"><input [(ngModel)]="showMoreControls";[ngModelOptions]={standalone: true}">
看起来在 RC5 中两者不能再一起使用,但我找不到替代解决方案.
这是产生异常的组件:
<option *ngFor="let c of原因";[value]=c.text">{{c.text}}</选择>
答案就在错误信息上,您需要指出它是独立的,因此它与表单控件不冲突:
[ngModelOptions]={standalone: true}";
After upgrading to RC5 we began getting this 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>
It looks like in RC5 the two can no longer be used together, but I could not find an alternative solution.
Here is the component producing the exception:
<select class="field form-control" [formGroup]="form" [(ngModel)]="cause.id" [name]="name">
<option *ngFor="let c of causes" [value]="c.text">{{c.text}}</option>
</select>
The answer is right on the error message,you need to indicate that it's standalone and therefore it doesn't conflict with the form controls:
[ngModelOptions]="{standalone: true}"
这篇关于ngModel 不能用于使用父 formGroup 指令注册表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!