禁用值不呈现

Disabled value is not rendering

通过模板设置禁用属性工作正常,但控制台显示此警告:

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

所以,我的解决方案是通过在后面编码(.ts 文件):

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

调试代码我可以确认控件(省)正在将其状态从禁用更改为启用等等,但是 html 没有渲染它values/changes.

也尝试过:

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

和:

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

或:

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

提前致谢....

在@trichetriche 帮助下发布解决方案。希望它能像帮助我一样帮助别人。

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

[禁用]="boolean"(真或假)

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