我试图实施什么似乎是一个简单的形式验证例程this walk-through我的离子2项目。Ionic 2表单验证问题与formGroup和ngModel
在我的控制器的构造函数我用FormBuilder
这样做出formGroup:
this.form = formBuilder.group({
date: ['', Validators.required],
client: ['', Validators.required]
});
然后在模板我加formControllerName
属性相关内容是这样的:
<ion-select formControlName="client" [(ngModel)]="clientId">
和绑定像这样的“formGroup”的根元素:
<ion-content [formGroup]="form">
这失败,出现以下错误消息:
ngModel不能使用与父 formGroup指令登记表单控件。请尝试使用 formGroup的合作伙伴指令“formControlName”。例如:
<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>
正如所建议的错误信息,以及this StackOverflow thread我说[ngModelOptions]="{standalone: true}"
到我的输入,但它与其他错误消息回来:
模板解析错误:不能绑定到'ngModelOptions',因为它不是'离子选择'的已知属性 。
你为什么同时使用'formControlName'和'ngModel'? – Amit
@Amit是否(formControlName)也做绑定?如果可以,请发表一个解释我如何访问绑定值的答案? – Vahid
是的,我会:) – Amit