相关疑难解决方法(0)

ngModel不能用于使用父formGroup指令注册表单控件

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

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>
Run Code Online (Sandbox Code Playgroud)

看起来在RC5中两者不能再一起使用,但我找不到替代解决方案.

以下是产生异常的组件:

<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>
Run Code Online (Sandbox Code Playgroud)

angular2-forms angular2-formbuilder angular

66
推荐指数
6
解决办法
7万
查看次数