如何从表单组中禁用的表单控件中获取值?

How to get values from disabled form controls in a form group?

我尝试使用表单状态对象初始化我的新 FormControl,然后我注意到此控件不会影响我的表单验证,它也会从 FormGroup 值中消失。

this.userForm = new FormGroup({
  email: new FormControl('', Validators.required),
  firstName: new FormControl('',Validators.required),
  lastName: new FormControl('',Validators.required),
  role: new FormControl({value: 'MyValues', disabled: true},Validators.required),
 })

现在,如果我尝试这样做:

this.userForm.value //email, firstName, lastName

有人遇到过这个问题吗?任何解决方案? Angular版本:5.2.6

这不是问题,是预期的行为。如果您想包括所有值而不考虑禁用状态,请使用以下内容:

this.userForm.getRawValue()

谢谢 @jota-toledo 为我提供了我需要的 80%。

对于那些正在寻找相同问题的解决方案但对于嵌套表单我能够通过更改我通常的方法来解决的人

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

this.userForm.getRawValue().nestedForm

TS 解决方案:

this.userForm.getRawValue()

HTML只有解决方案:

<input formControlName="name" [readonly]="condition">

CSS只有解决方案:

pointer-events: none;

Html 和 Css 解决方案将阻止用户与输入交互,但 Reactive 表单将获得值

如果有人正在寻找在 FormGroup[=18= 中获取 FormArray 的禁用 FormControl 的解决方案].

试试这个 - (this.formName.controls['formArrayName'] as FormGroup).getRawValue();