【发布时间】:2017-06-30 01:25:57
【问题描述】:
我有一个反应形式。在编辑时,我希望禁用控件。
以下工作:
this.myForm.controls['analysis_horizon'].disable();
但是,key analysis_horizon 不再在我的 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)
});
我在编辑时将数据库中的数据加载到表单控件中,但我需要一个不允许更改的字段。
【问题讨论】:
-
它告诉你直接在 HTML 中使用 disabled 属性,而不是与 formcontrol 一起使用。
-
鉴于它没有改变,你可以自己把它放回价值,无论是什么消耗它,
Object.assign或其他东西。 -
也许我理解错了,但是,为什么不按照消息建议的那样做:
yourFormControl: new FormControl({value: yourPresetValue, disabled: true}) -
我知道这已经过时了,但是响应式表单有另一种称为 form.getRawValue() 的方法可以为您执行此操作我不能说它在 100% 的时间内有效,但是当您使用任何 setValue 或 patch Value 方法
标签: angular