简体   繁体   English

angular2 rc6-如何在事件中更改反应性FormControl值

[英]angular2 rc6 - how to change reactive FormControl value on event

This is how I set my form in rc6: 这是我在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]]
        })

So on form changes I subscribe to events and based on a condition I want to change a disabled field from true to false: 因此,在表单更改中,我订阅事件,并根据条件将禁用字段从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();

For example if I set the below to false I get the below error 例如,如果我将以下设置为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

您可以使用AbstractControl的enable()和disable()方法

this.myForm.controls.lambda.enable();

You use: this.myForm.value.lambda to get the value of the field in question. 您使用: this.myForm.value.lambda来获取有关字段的值。 To set the value do: 要设置值,请执行以下操作:

(<FormControl> this.myform.controls['lambda']).setValue(false);

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

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