Home > Mobile >  How to pass boolean property value to angular forms disabled attribute?
How to pass boolean property value to angular forms disabled attribute?

Time:12-10

I'm trying something like this to disable a form control dynamically based on a property value:

  pessoaFisica: boolean = true

      cnpj: [{ value: '', disabled: this.pessoaFisica }, [Validators.required, Validators.minLength(14), Validators.maxLength(14)]],

I tested passing a property value to angular forms value property and it worked. Why it doenst work for disabled?

CodePudding user response:

Try to create the control of the form to FormControl:

cnpj: new FormControl(
  { value: '', disabled: this.pessoaFisica },
  [Validators.required, Validators.minLength(14), Validators.maxLength(14)]
),

If it doesn't work, check your html input, could be any remaining property that overwrite the typescript file.

CodePudding user response:

You can use readonly instead of disabled in your template

<input [readonly]="property" type="text" name="control">
  • Related