从反应性角形式中删除场的最佳方法



我想在更新时不要从我的反应式 Angular 6 表单发送字段,因为我正在使用的 API 在更新中不处理这个"额外"字段。

当我添加我的对象时,这就是我发送的内容:

{"人": {"姓名":"约翰", "地址": "2050巴马科广场华盛顿特区 20521-2050", "代码": "256222"}

当我更新我的表单时,我想要(没有更多的"代码"(:

{"人": {"姓名":"约翰", "地址": "2051 巴马科广场华盛顿特区 20521-2051"}

我尝试为我的字段使用"禁用",但它仍然提交我不想要的字段。我也尝试了只读但与上面相同。我不知道实现这一目标的最佳方法是什么。

形式.html

<form [formGroup]="testForm" (ngSubmit)="formValidate()" >
<mat-card-content class="mat-card-content">
<div fxLayout="row">
<div fxLayout="column" fxFlex="100%">
<mat-form-field class="form-input"> 
<input matInput formControlName="name" required
placeholder="Name" /> 
</mat-form-field>
<mat-form-field class="form-input">
<textarea matInput formControlName="address" required
placeholder="Address" rows="4" cols="200"></textarea>
</mat-form-field>
<mat-form-field class="form-input"> 
<input matInput formControlName="code" required
placeholder="Code" /> 
</mat-form-field>
</div>
</div>
</mat-card-content>
<mat-card-footer style="margin:0 auto;">
<div fxLayout="row" fxLayoutAlign="end center">
<button type="submit" color="primary" mat-raised-button [disabled]="testForm.invalid">Submit</button>
</div>
</mat-card-footer>
</form>

表单.ts

this.testForm = fb.group({
'name': fb.control('', [Validators.required]),
'address': fb.control('', [Validators.required]),
'code': fb.control('', [Validators.required]), // I would like to do not send this field when I'm updating my form.
});

formValidate() {
this.person = this.testForm.value;
if (this.updateForm) {
this.person['id'] = this.currentId;
}
console.log(this.person);
// create or update by sending object to the API
...
}

谢谢!

试试这个:this.testForm.removeControl('code');您还可以创建一个名为 ie 的对象。request并放置要发送的值。

最新更新