0

I have the following control on my Angular reactive form:

name: this.name = this.fb.control({ value: '', disabled: this.isNameDisabled }, this.nameValidations )

isNameDisabled is a getter as following:

private get isNameDisabled(): boolean {
    return this.isDisabled;
}

The first load works correctly, but when updating this.isDisabled to True, disabled property is not updated to true, and the input is still enabled.

Is there any way to dynamically disable the input without using this.name.disable()?

3
  • 1
    Have you tried by adding disabled property in HTML? Commented Dec 5, 2022 at 9:20
  • @VinceCyriac not a good practice in reactive forms Commented Dec 5, 2022 at 9:23
  • > Is there any way to dynamically disable the input without using this.name.disable()? No. Commented Dec 5, 2022 at 9:30

3 Answers 3

1

In this case name: this.name = this.fb.control({ value: '', disabled: this.isNameDisabled }, this.nameValidations ) will work only to initial behavior, if you change the value of this.isNameDisabled will no reflect to the control.

To what you need the best approach is this.form.controls['name'].disable();

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

1 Comment

Without form.controls['name'], just this.name.disable() to disable and this.name.enable() to enable.
0

what is the problem with calling this.name.disable() ?

You could use a directive:

import { NgControl } from '@angular/forms';

@Directive({
  selector: '[customDisable]'
})
export class CustomDisableDirective {

  @Input() set customDisable( isDisabled: boolean ) {
    const action = isDisabled ? 'disable' : 'enable';
    this.ngControl.control[action]();
  }

  constructor( private ngControl : NgControl ) {
  }

}

Then use it inside your HTML:

<input [formControl]="formControl" [customDisable]="isNameDisabled()">

2 Comments

You should not call methods from template
We should not call methods from template because it is called every time Angular change detection runs. It could be an issue if the function is time consuming. Here we are talking about a boolean then it is ok.
0

You can also do it in template by assigning a variable to a disabled attribute. In that case you'd have a method which sets disabled variable like so:

isNameDisabled = false;

ngOnInit(){
  this.setNameDisabled();
}

setNameDisabled() {
  // this is just an example, you should replace it with any logic that you have
  this.isNameDisabled = true;
}

And template should look as following:

<input [formControl]="formControl" [attr.disabled]="isNameDisabled">

2 Comments

If you use [disabled] attribute and reactive form you will get this warning message: "It looks like you're using the disabled attribute with a reactive form directive ...". The CustomDisableDirective above will not rise any warning / error.
In that case you can use [attr.disabled] instead of [disabled] to solve the problem. Btw this is the most simple solution, I agree that custom directive might be better option, of course, dependent of use case

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.