从 formBuilder 中删除父字段 angular 4
Delete parent field from formBuilder angular 4
我想从我的表单中删除父字段
这是我的例子:
initForm() {
return this.myForm= this.formBuilder.group({
startDate: [this.data.startDate?this.data.startDate:new Date().toISOString(),Validators.compose([Validators.required])],
dateDeposit: [this.data.dateDeposit?this.data.dateDeposit:new Date().toISOString(),Validators.compose([Validators.required])],
comment: [this.data.comment?this.data.comment:"",Validators.compose([Validators.required])],
firstName: [this.data.firstName?this.data.comment:"",Validators.compose([Validators.required])],
lastName: [this.data.lastName?this.data.comment:"",Validators.compose([Validators.required])],
adress: [this.data.adress?this.data.comment:"",Validators.compose([Validators.required])],
remarks: [this.data.remarks?this.data.remarks:"",Validators.compose([Validators.required])],
otherRemarks: [this.data.otherRemarks?this.data.otherRemarks:"",Validators.compose([Validators.required])],
contactClient: [this.data.contactClient?this.data.contactClient:"",Validators.compose([Validators.required])],
clientNumber: [this.data.clientNumber?this.data.clientNumber:"",Validators.compose([Validators.required])],
});
}
然后我将 this.myForm.value 作为一个对象发送到我的 HTTP 请求中。
this.myService.putForm(this.myForm.value).subscribe();
问题是我不想发送 dateDeposit。
发送 myForm.value 时如何从 myForm 中删除 DateDeposit?
您可以只发送 startDate
。尝试:
this.myService.putForm(this.myForm.get('startDate').value).subscribe();
或
this.myService.putForm(this.myForm.controls['startDate'].value).subscribe();
如果您想使用所有其他值,但 dateDeposit
,您可以:
// get form value without dateDeposit
let formValue = Object.assign({}, this.myForm.value);
delete formValue.dateDeposit;
// submit form value without dateDeposit
this.myService.putForm(formValue).subscribe();
你可以简单地使用
this.myService.putForm(this.myForm.value.startDate).subscribe();
this.myForm.removeControl('depositDate')
https://angular.io/api/forms/FormGroup#removeControl
您可以简单地从表单本身删除控件,但请注意它可能会在模板中产生问题,因为有一个输入标签绑定到此 formControl
我想从我的表单中删除父字段
这是我的例子:
initForm() {
return this.myForm= this.formBuilder.group({
startDate: [this.data.startDate?this.data.startDate:new Date().toISOString(),Validators.compose([Validators.required])],
dateDeposit: [this.data.dateDeposit?this.data.dateDeposit:new Date().toISOString(),Validators.compose([Validators.required])],
comment: [this.data.comment?this.data.comment:"",Validators.compose([Validators.required])],
firstName: [this.data.firstName?this.data.comment:"",Validators.compose([Validators.required])],
lastName: [this.data.lastName?this.data.comment:"",Validators.compose([Validators.required])],
adress: [this.data.adress?this.data.comment:"",Validators.compose([Validators.required])],
remarks: [this.data.remarks?this.data.remarks:"",Validators.compose([Validators.required])],
otherRemarks: [this.data.otherRemarks?this.data.otherRemarks:"",Validators.compose([Validators.required])],
contactClient: [this.data.contactClient?this.data.contactClient:"",Validators.compose([Validators.required])],
clientNumber: [this.data.clientNumber?this.data.clientNumber:"",Validators.compose([Validators.required])],
});
}
然后我将 this.myForm.value 作为一个对象发送到我的 HTTP 请求中。
this.myService.putForm(this.myForm.value).subscribe();
问题是我不想发送 dateDeposit。 发送 myForm.value 时如何从 myForm 中删除 DateDeposit?
您可以只发送 startDate
。尝试:
this.myService.putForm(this.myForm.get('startDate').value).subscribe();
或
this.myService.putForm(this.myForm.controls['startDate'].value).subscribe();
如果您想使用所有其他值,但 dateDeposit
,您可以:
// get form value without dateDeposit
let formValue = Object.assign({}, this.myForm.value);
delete formValue.dateDeposit;
// submit form value without dateDeposit
this.myService.putForm(formValue).subscribe();
你可以简单地使用
this.myService.putForm(this.myForm.value.startDate).subscribe();
this.myForm.removeControl('depositDate')
https://angular.io/api/forms/FormGroup#removeControl
您可以简单地从表单本身删除控件,但请注意它可能会在模板中产生问题,因为有一个输入标签绑定到此 formControl