FormControlName TypeError:无法读取属性' value'未定义的

时间:2017-10-31 13:35:29

标签: angular ionic-framework formgroups

我的离子项目中有一个下拉项目

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>

使用formgroup创建表单 的 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'];
 }

当尝试获取位置值获取错误消息为&#39; undefined&#39;

doprint() {

console.log( this.name.value)   //Ajith
console.log(this.email.value)    //ajith@gmail.com
console.log(this.location.value) //undefined
}

1 个答案:

答案 0 :(得分:0)

你有一个错字

this.location=this.formgroup.controls['location'];

无论如何,标准的做法是,

doprint() {
let formItem = this.itemForm.value;
console.log(formItem.name);
console.log(name.email);
console.log(name.location) ;
}