2

I'm using reactive form in my angular application.

measurement: new FormControl(true),
volume: new FormControl(0)

And in my html i'm using like

<input type="checkbox" formControlName='measurement' #measurement>Volume
<input formControlName='volume' type="number" class="form-control input-md" [attr.disabled]="!measurement.checked">

When ever I check 'Volume' check box it should enable 'volume' control. But it is not switching between enable and disable. But if I console 'measurement.checked' it is show true or false. How can I do enable or disable via local reference? Thank you.

2 Answers 2

2

Disabled cannot be Set Dynamically like your approach for more info https://github.com/angular/angular/issues/11271 . But you can enable or disable via function in onchange like control.enable() , control.disable()

Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve it through typescript function..

Change the below line in your code,

<input formControlName='volume' type="number" class="form-control input-md" [attr.disabled]="!measurement.checked">

to

<input formControlName='volume' type="number" class="form-control input-md" [attr.disabled]="disableVolume()">

and in your ts file, add the function disableVolume(),

  disableVolume() {
    if(this.form.get('measurement').value === true) {
      this.form.get('volume').enable();
    } else {
      this.form.get('volume').disable();
    }
  }

Working example: https://stackblitz.com/edit/angular-form-array-example-cc27c3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.