26

I have a select control that I want to disable dynamically based on a condition:

this.activityForm = this.formBuilder.group({
  docType: [{ value: '2', disabled: this.activeCategory != 'document' }, Validators.required]
});

However, docType doesn't become enabled even though at some point this.activeCategory becomes 'document'.

How do I fix this?

5 Answers 5

47

Since I don't know how you're manipulating activeCategory (maybe it's also a FormControl?), I'll suggest the following approach:

You can use (change) to detect when this.activeCategory has changed, as below:

1 - If you're using ngModel:

<input type="text" [(ngModel)]="activeCategory" (change)="checkValue($event)">

2 - If it's a FormControl:

<input type="text" formControlName="activeCategory" (change)="checkValue($event)">

So, in component, you can manipulate the docType control using disable/enable methods:

checkValue(event: Event) {
  const ctrl = this.activityForm.get('docType');

  if (event.value === 'document') {
    ctrl.enable();
  } else {
    ctrl.disable();
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

On option 2: if it's a FormControl I recommend subscribing to ``` this.form.get('activeCategory').valueChanges ``` instead of putting logic on DOM.
This doesn't make it truly dynamic: everywhere activeCategory is changed would need to call for checkValue() manually. You're assuming that activeCategory may only be edited by the input it's on (which seems to have been the case since OP accepted the answer), but then this wouldn't work in a scenario where it's edited from other functions or components.
10

You have to handle select elements differently than you do other HTML elements. You will have to perform a reset when this.activeCategory changes.

Something like this:

this.activityForm.controls['docType'].reset({ value: '2', disabled: false });

Of course, you will probably want to use the current value, rather than a hard coded '2'.

Same sort of thing to disable it, if that need arises (and it probably will).

this.activityForm.controls['docType'].reset({ value: '2', disabled: true });

More information on ng form control reset.

2 Comments

Is there a nicer way of doing this? With say setValue or patchValue?
@Kody For enabling/disabling a select, not that I am aware of. setValue and patchValue are for changing the form controls value, not visual state.
1

from .ts file you can add keys to yours from controls, and there you add the item disabled:true (desactivate form item) or false (activate form item)

.ts

public form_name=new FormGroup({

              series: new FormControl({value: '', disabled: true}),
              inten: new FormControl({value: '', disabled: true}),
              rep: new FormControl({value: '', disabled: true}),
              rest: new FormControl({value: '', disabled: false}),
              observation: new FormControl({value: '', disabled: false}),



  });

.html

<form  [formGroup]="form_name" >

                                          <label for="series"  >Series</label>
                                           <input type="text" formControlName="series" >

                                          <label for="inten"  >Inten</label>
                                           <input type="text" formControlName="inten" >

                                          <label for="rep"  >Rep</label>
                                           <input type="text" formControlName="rep" >

                                           <label for="rest"  >rest</label>
                                            <textarea formControlName="rest" rows="3" cols="30" ></textarea>


                    </form>

Comments

1
this.activityForm.controls['docType'].disable();

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.
-4

you can use ternary operator here to disable form control conditionally.

this.activityForm = this.formBuilder.group({
docType: [{ value: '2', disabled: this.activeCategory != 'document' ? true : false }, Validators.required]
});


1 Comment

This is exactly the same code as OP posted, but with an additional, useless ? true : false added on top of a statement that's already a boolean. Please consider giving an actual solution, as OP wants the change to be dynamic (which neither their original code nor yours is).

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.