我試圖實施什麼似乎是一個簡單的形式驗證例程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