Search code examples
angularangular7angular-ngselect

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....


Solution

  • 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();
          }