Eduardo Nunes
Eduardo Nunes

Reputation: 19

How to pass boolean property value to angular forms disabled attribute?

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?

Upvotes: 0

Views: 578

Answers (2)

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.

Upvotes: 2

voq
voq

Reputation: 73

You can use readonly instead of disabled in your template

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

Upvotes: 0

Related Questions