user630209
user630209

Reputation: 1207

How to check whether a form control is disabled or not in reactive forms

Is there a way in angular to check whether a form control is disabled ?

this.form.get("nationality").disable()

disabled this control.

Can i check whether this control is disable on reset method. If its disabled , don't need to reset the value.

How can I check that.

onReset(){
 if(this.form.get("nationality").isDisable()){// its wrong
let name =this.form.get("nationality").value;
}else{
let name = null;
}
 this.form.reset({
        name: name
    });

}

Upvotes: 7

Views: 12216

Answers (2)

Bullsized
Bullsized

Reputation: 577

Log your FormControl, it has a property called disabled:

enter image description here

which you can check like so:

// this is the control that we're using
formControl: FormControl = new FormControl('');

// and this is the check that we're doing:
if (this.formControl.disabled) {
  // do your magic here
}

Upvotes: 2

user13258211
user13258211

Reputation:

Form controls do have a disabled property, so for example

if(!myForm.controls['myControl'].disabled){
  myForm.controls['myControl'].reset();
}

will only reset the form control if it was not disabled.

See https://angular.io/api/forms/FormControl for more information

Upvotes: 7

Related Questions