1
嗨,我得到這個error
爲什麼表單驗證是給這個錯誤錯誤:ngModel不能用於與父母formGroup指令
Error: ngModel cannot be used to register form controls with a parent formGroup directive
我試圖做基本驗證了form
註冊表單控件inputs
這裏是我的form
<form [formGroup]="formgroup">
<ion-item>
<ion-label floating>First Name</ion-label>
<ion-input type="text" formControlName="firstname" [(ngModel)]= "userData.firstname" ></ion-input>
</ion-item>
<ion-item *ngIf="firstname.hasError('required') && firstname.touched">
<p>*First Name Is Required</p>
</ion-item>
<ion-item>
<ion-label floating>Last Name</ion-label>
<ion-input type="text" formControlName="lastname" [(ngModel)]= "userData.lastname" ></ion-input>
</ion-item>
<ion-item *ngIf="lastname.hasError('required') && lastname.touched">
<p>*First Name Is Required</p>
</ion-item>
</form>
這裏是我的控制器代碼
import {FormBuilder,FormGroup,Validators,AbstractControl,FormControl} from '@angular/forms';
export class Form1Page {
formgroup:FormGroup;
firstname:AbstractControl;
lastname:AbstractControl;
userData = {"firstname":"", "lastname":""};
constructor(public navCtrl: NavController, public navParams: NavParams,public formBuilder:FormBuilder) {
this.formgroup = formBuilder.group({
firstname:['',Validators.required,Validators.minLength(5)],
lastname:['',Validators.required,Validators.maxLength(15)]
});
this.firstname = this.formgroup.controls['firstname'];
this.firstname = this.formgroup.controls['lastname'];
this.userData.dateOfBirth = new Date().toISOString();
}
}
請幫我擺脫這些error
感謝提前!
亞那是正確的+1,但現在我的錯誤已經改變爲這個錯誤:ngModel不能用於註冊表單控件與父formGroup指令 – EaB
亞那是正確的,但我是一個總的初學者與'離子3',請幫助我更新回答 – EaB
https://stackblitz.com/edit/ionic-ejqbwz?file=pages/home/home.html'Validators.minLength(5)'是他的情況下的異步驗證程序。我使用了'Validators.compose'來組合同步驗證器。但我沒看到他在說的錯誤 – yurzui