在我的angular2代码中,我想更新表单的值,但我的点击功能不起作用

时间:2016-11-04 06:47:33

标签: angular angular2-template angular2-forms

在我的angular2代码中,我想更新表单的值但我的点击功能不起作用。

我的代码是:

<div *ngIf="payLoad" class="form-row">    
    <li (click)="select()">{{payLoad}}</li>   
</div> 

我的主要成分是:

select(payLoad) {     
    this.form.value.external_id=payLoad;   
}    

onSubmit()  {
    if (this.form.valid) {
        for(let fm in this.form.value) { 
            if(fm == 'external_id') {
                this.payLoad=this.form.value.external_id;
            }   
            if(fm == 'lastname') {
                this.payLoad1=this.form.value.lastname; 
            }
            if(fm == 'email') { 
                this.payLoad2=this.form.value.email;
            } 
            if(fm == 'gender') { 
                this.payLoad3=this.form.value.gender;
            } 
            if(fm=='brave'){ 
                this.payLoad4=this.form.value.brave;
            }  
            if(fm=='role') { 
                this.payLoad5=this.form.value.role; 
            } 
            if(fm=='status') {
                this.payLoad6=this.form.value.status;           
            }       
        }            
    }     
}  

1 个答案:

答案 0 :(得分:0)

在Angular2中,FormGroup仅通过.value公开其值,您无法通过它更新FormGroup的值。 您需要使用.setValue(value).patchValue(value)

  • 如果要使用一个对象设置整个组的值,请使用.setValue(value)(它必须与FormGroup的架构匹配。
  • 如果您想使用与.patchValue(value)架构匹配的部分对象仅设置组值的子集,请使用FormGroup