gpt4 book ai didi

angular - 如何禁用表单控件但保持值(value)

转载 作者:太空狗 更新时间:2023-10-29 16:52:25 25 4
gpt4 key购买 nike

我有一个 react 形式。在编辑时我想禁用一个控件。

以下作品:

   this.myForm.controls['analysis_horizon'].disable();

但是,关键 analysis_horizo​​n 不再在我的 myForm.value 散列中。

如何禁用具有反应式表单的字段但将值保留在表单值哈希中?

我试过 [disabled]= 但我得到以下信息:

It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true
when you set up this control in your component class, the disabled attribute will actually be set in the DOM for
you. We recommend using this approach to avoid 'changed after checked' errors.

Example:
form = new FormGroup({
first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
last: new FormControl('Drew', Validators.required)
});

我在编辑时将数据库中的数据加载到表单控件中,但我需要一个字段不允许更改。

最佳答案

您可以使用 getRawValue() 代替 value 属性。根据documentation :

The aggregate value of the FormGroup, including any disabled controls.

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.

this.myForm.getRawValue()

关于angular - 如何禁用表单控件但保持值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42180696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com