简体   繁体   English

Angular 2 禁用控件未包含在 form.value 中

[英]Angular 2 disabled controls do not get included in the form.value

I have noticed that if I disable a control on an Angular 2 reactive form then the control does not get included in the form.value.我注意到,如果我在 Angular 2 反应式表单上禁用控件,则该控件不会包含在 form.value 中。 For example, if I define my form like below:例如,如果我定义我的表单如下:

this.notelinkingForm = new FormGroup({
    Enabled: new FormControl(settings.Enabled, Validators.required),
    LinkToPreceeding: new FormControl({value: settings.LinkToPreceeding, disabled: !settings.Enabled}, Validators.required),
    LinkingTolerance: new FormControl({value: settings.LinkingTolerance, disabled: !settings.Enabled}, Validators.required)
});

and check the this.notelinkingForm.value, if all the controls are enabled then output would be:并检查 this.notelinkingForm.value,如果启用了所有控件,则输出将是:

{"Enabled":true, "LinkToPreceeding": true, LinkingTolerance:"100"} 

However, when some of the controls are disabled it would be:但是,当某些控件被禁用时,它将是:

{"Enabled":true} 

Notice how the disabled controls are excluded.请注意如何排除禁用的控件。

My intent is that when the form changes I want to be able to pass the form.value with all the properties in it off to my rest API.我的意图是,当表单更改时,我希望能够将包含所有属性的 form.value 传递给我的其余 API。 This won't be possible obviously if it does not contain the disabled items.如果它不包含禁用的项目,这显然是不可能的。

Am I missing something here or is this the expected behavior?我在这里遗漏了什么还是这是预期的行为? Is there a way to tell Angular to include the disabled items in the form.value?有没有办法告诉 Angular 在 form.value 中包含禁用的项目?

Welcome your thoughts.欢迎你的想法。

You can use:您可以使用:

this.notelinkingForm.getRawValue()

From Angular docs :来自Angular 文档

If you'd like to include all values regardless of disabled status, use this method.如果您想包括所有值而不考虑禁用状态,请使用此方法。 Otherwise, the value property is the best way to get the value of the group.否则, value属性是获取组值的最佳方式。

我使用的另一个选项是:

this.form.controls['LinkToPreceeding'].value;

Thank you @Sasxa for getting me 80% what I needed.感谢@Sasxa为我提供了 80% 的需求。

For those of you looking for a solution to the same problem but for nested forms I was able to solve by changing my usual对于那些正在寻找相同问题的解决方案但对于嵌套表单的人,我可以通过改变我的习惯来解决

this.notelinkingForm.get('nestedForm').value

to

this.notelinkingForm.getRawValue().nestedForm

If you use readonly instead of disabled it's still not editable while the data will be included in the form.如果您使用readonly而不是disabled它仍然不可编辑,而数据将包含在表单中。 But that isn't possible in all cases.但这并非在所有情况下都是可能的。 Eg it's not available for radio buttons and checkboxes.例如,它不适用于单选按钮和复选框。 See MDN web docs .请参阅MDN 网络文档 In those cases you have to apply for the other solutions provided here.在这些情况下,您必须申请此处提供的其他解决方案。

There's a two way we can get disabled form values.有两种方法可以获得禁用的表单值。 First第一的

 onSubmit(){
 for (const prop in this.formControl.controls) {
      this.formControl.value[prop] = this.formControl.controls[prop].value;
    }
}

Second way You can enable form at onSubmit event第二种方式您可以在 onSubmit 事件中启用表单

 onSubmit(){
     this.formControl.enable() 
   //Your logical/operational statement goes here

  //at last if you wish you can again disable your form like this
   this.formControl.disable();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM