ngModel不能用于通过父formGroup指令注册表单控件 [英] ngModel cannot be used to register form controls with a parent formGroup directive

查看:746
本文介绍了ngModel不能用于通过父formGroup指令注册表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到RC5后,我们开始出现此错误:

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>

在RC5中,看起来这两个不能再使用了,但是我找不到其他选择解决方案。

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>


推荐答案

错误消息$ b的答案正确$ b您需要指出它是独立的,因此与表单控件不冲突:

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指令注册表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆