FormControlName TypeError: Cannot read property 'value' of undefined
FormControlName TypeError: Cannot read property 'value' of undefined
我的 ionic 项目中有一个下拉项
home.html
<ion-list>
<ion-item>
<ion-label color="danger">Location</ion-label>
<ion-select name="location" formControlName="location" >
<ion-option value=3>{{'Palarivattom'}}</ion-option>
<ion-option value=4>{{'Kadavantra'}}</ion-option>
<ion-option value=5>{{'Kaloor'}}</ion-option>
</ion-select>
</ion-item>
</ion-list>
使用表单组创建表单
home.ts
{
this.formgroup=formbuilder.group({
mobile:['', Validators.compose([Validators.required,
Validators.minLength(10,), Validators.maxLength(10)])],
password:['', Validators.compose([Validators.required,
Validators.minLength(5)])],
location:['', Validators.compose([Validators.required])],
});
this.mobile=this.formgroup.controls['mobile'];
this.password=this.formgroup.controls['password'];
this.location=this.formgroup.controls['loaction'];
}
当试图获取位置值时收到错误消息 'undefined'
doprint() {
console.log( this.name.value) //Ajith
console.log(this.email.value) //ajith@gmail.com
console.log(this.location.value) //undefined
}
你打错了
this.location=this.formgroup.controls['location'];
无论如何,标准的做法是,
doprint() {
let formItem = this.itemForm.value;
console.log(formItem.name);
console.log(name.email);
console.log(name.location) ;
}
我的 ionic 项目中有一个下拉项
home.html
<ion-list>
<ion-item>
<ion-label color="danger">Location</ion-label>
<ion-select name="location" formControlName="location" >
<ion-option value=3>{{'Palarivattom'}}</ion-option>
<ion-option value=4>{{'Kadavantra'}}</ion-option>
<ion-option value=5>{{'Kaloor'}}</ion-option>
</ion-select>
</ion-item>
</ion-list>
使用表单组创建表单 home.ts
{
this.formgroup=formbuilder.group({
mobile:['', Validators.compose([Validators.required,
Validators.minLength(10,), Validators.maxLength(10)])],
password:['', Validators.compose([Validators.required,
Validators.minLength(5)])],
location:['', Validators.compose([Validators.required])],
});
this.mobile=this.formgroup.controls['mobile'];
this.password=this.formgroup.controls['password'];
this.location=this.formgroup.controls['loaction'];
}
当试图获取位置值时收到错误消息 'undefined'
doprint() {
console.log( this.name.value) //Ajith
console.log(this.email.value) //ajith@gmail.com
console.log(this.location.value) //undefined
}
你打错了
this.location=this.formgroup.controls['location'];
无论如何,标准的做法是,
doprint() {
let formItem = this.itemForm.value;
console.log(formItem.name);
console.log(name.email);
console.log(name.location) ;
}