在我的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;
}
}
}
}
答案 0 :(得分:0)
在Angular2中,FormGroup
仅通过.value
公开其值,您无法通过它更新FormGroup
的值。
您需要使用.setValue(value)
或.patchValue(value)
。
.setValue(value)
(它必须与FormGroup
的架构匹配。.patchValue(value)
架构匹配的部分对象仅设置组值的子集,请使用FormGroup
。