【问题标题】:in my angular2 code i want to update value of form but my click function is not working在我的 angular2 代码中,我想更新表单的值,但我的点击功能不起作用
【发布时间】:2017-03-17 22:37:16
【问题描述】:

在我的 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;           
            }       
        }            
    }     
}  

【问题讨论】:

  • 欢迎来到 StackOverflow。我们试图在这里保持一些质量。请收拾烂摊子。您问题中的代码不可读。

标签: angular angular2-template angular2-forms


【解决方案1】:

在 Angular2 中,FormGroups 只能通过.value 公开其值,您不能通过它更新FormGroup 的值。 您需要使用.setValue(value).patchValue(value)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2020-10-17
    • 2014-03-29
    • 2017-02-18
    • 2021-03-30
    相关资源
    最近更新 更多