我正在将代码从角度beta迁移到RC5版本.
我面临着使用的基于模型的表格的问题.
因为我已经在角度2 beta中开发了很多形式.我很难将基于模型的表单更改为基于模板的表单.
任何形式迁移的帮助都非常感谢.
我面临着使用的基于模型的表格的问题.
因为我已经在角度2 beta中开发了很多形式.我很难将基于模型的表单更改为基于模板的表单.
任何形式迁移的帮助都非常感谢.
我现有的代码是这样的
profile.ts
import {FORM_DIRECTIVES,REACTIVE_FORM_DIRECTIVES} from '@angular/forms'; import {FormBuilder,FormGroup,FormControl,Validators} from '@angular/forms'; this.firstName = new FormControl(); this.lastName = new FormControl(); this.email = new FormControl(); this.addressLine = new FormControl(); this.postal = new FormControl(); this.postalArea = new FormControl(); this.form = builder.group({ firstName: this.firstName,lastName: this.lastName,email: this.email,addressLine: this.addressLine,postal: this.postal,postalArea: this.postalArea,photo: this.photo });
和profile中的模板.HTML如下
<form class="form-default" [formGroup]="form"> <input type="text" class="form-control" id="firstName" [(ngModel)]="model.firstName" formControlName="firstName" maxlength="200"> <input type="text" class="form-control" id="lastName" [(ngModel)]="model.lastName" formControlName="lastName" maxlength="200"> <input type="text" class="form-control" id="email" [(ngModel)]="model.username" readonly formControlName="email" maxlength="100"> </form>
我在控制台中遇到以下错误.
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in
/assets/scripts/my-profile/my-profile.html:176:66 ORIGINAL EXCEPTION:
ngModel cannot be used to register form controls with a parent formGroup directive. Try using
formGroup’s partner directive “formControlName” instead. Example:06002