【问题标题】:angular2 rc6 - how to change reactive FormControl value on eventangular2 rc6 - 如何更改事件的响应式 FormControl 值
【发布时间】:2017-01-12 15:33:18
【问题描述】:

这就是我在 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

【问题讨论】:

    标签: angular angular2-forms


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您使用: this.myForm.value.lambda 获取相关字段的值。要设置值,请执行以下操作:

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

      【讨论】:

      • 所以如果我添加 this.myForm.controls['lambda'].setValue(false);在 if cond 永远循环然后应用程序崩溃
      • 如何将禁用设置为 false?
      • 这适用于设置值 this.myForm.controls['lambda'].setValue(.88,{disabled: false});但没有更改禁用标志
      • 我想我必须使用它,但我不知道如何:angular.io/docs/ts/latest/api/forms/index/… registerOnDisabledChange(fn: (isDisabled: boolean) => void) : void
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 2016-11-26
      • 2021-01-25
      • 2017-12-10
      相关资源
      最近更新 更多