Kenzo_Gilead
Kenzo_Gilead

Reputation: 2439

Disabled value is not rendering

Setting disabled attribute by template works correctly, but the console shows this warning:

disabled warning

<... ... name="Province" id="name="Province" formControlName="Province" [disabled]="this.listValues.length==0">

So, my solution coming through code it in the back (.ts file):

  this.listValues= regions;
  if (this.listValues.length === 0) {
    this.registerForm.get('Province').disable();
  } else {
    this.registerForm.get('Province').enable();
  }

Debugging the code I can confirm that the control (Province) is changing its state from disabled to enabled and so on, but the html is not rendering this values/changes.

Also tried:

<... ... name="Province" id="name="Province" formControlName="Province" [attr.disabled]="this.listValues.length==0">

and:

...
this.registerForm = this.formBuilder.group({
      Province: [{ value: '', disabled: this.listValues.length===0}, null],
....

or:

  this.listValues= regions;
  if (this.listValues.length === 0) {
    this.registerForm.controls['Province'].disable();
  } else {
    this.registerForm.controls['Province'].enable();
  }

Thanks in advance....

Upvotes: 0

Views: 344

Answers (2)

Julien Rousset
Julien Rousset

Reputation: 11

[disabled]="boolean" (true or false)

[disabled]="listValues.length == 0 ? true : false"

Upvotes: 0

Kenzo_Gilead
Kenzo_Gilead

Reputation: 2439

Posting solution after @trichetriche help. Hoping it helps others as it helps me.

HTML

....
<... ... name="Province" id="name="Province" formControlName="Province" [disabled]="registerForm.get('Province').disabled">

TS

.....
      this.listValues= regions;
      if (this.listValues.length === 0) {
        this.registerForm.get('Province').disable();
      } else {
        this.registerForm.get('Province').enable();
      }

Upvotes: 1

Related Questions