这是我在rc6中设置表单的方式:
this.myForm = fb.group({
lambda: new FormControl({value: .94, disabled: true}, Validators.required),
//lambda: ['',[Validators.required]],
radio11: ['es',[Validators.required]],
startdate: ['',[Validators.required]],
enddate: ['',[Validators.required]]
})
因此,在表单更改时,我订阅了事件,并根据我想要将禁用字段从true更改为false的条件:
this.myForm.valueChanges.do(formValue=>{
console.log(formValue);
console.log(formValue.radio11);
if (formValue.radio11=='es'){
console.log(this.myForm.controls.lambda.disabled);
}
}).subscribe();
例如,如果我将以下设置为false,则会出现以下错误
this.myForm.controls.lambda.disabled=false
core.umd.js:5995 EXCEPTION: Uncaught (in promise): Error: Error in app/model_parameters_general/model_parameters_general.component.html:19:27 caused by: Cannot set property disabled of #<AbstractControl> which has only a getter
答案 0 :(得分:3)
您可以使用AbstractControl的enable()和disable()方法
this.myForm.controls.lambda.enable();
答案 1 :(得分:0)
您使用:
this.myForm.value.lambda
获取相关字段的值。要设置值,请执行以下操作:
(<FormControl> this.myform.controls['lambda']).setValue(false);