NG-ZORRO / ng-zorro-antd

Angular UI Component Library based on Ant Design
https://ng.ant.design
MIT License
8.87k stars 3.94k forks source link

nz-input textarea 在 formControlName 下, disabled 属性不生效 #7167

Closed msyesyan closed 2 years ago

msyesyan commented 2 years ago

Reproduction link

https://stackblitz.com/edit/ng-zorro-antd-ivy-uda7th

Steps to reproduce

直接打开重现链接,点击按钮,应该可以切换 disabled 状态,但是并没有

What is expected?

点击按钮,可以 toggle disabled 状态

What is actually happening?

没反映

Environment Info
ng-zorro-antd 12.0.1
Browser Chrome

我们实际代码是 12.0.1, 但是 stackblitz 里是 11 版本,但其实都一样,12 也有同样问题,只不过那个 stackblitz 链接给的是 v11 的代码,自己换 12 又太麻烦了。我们代码中需要如下策略:

  1. OnPush 策略
  2. formControlName
zorro-bot[bot] commented 2 years ago

Translation of this issue:

NZ-INPUT TEXTAREA Under FormControlName, the Disabled attribute does not take effect.

reproduction link

[https://stackblitz.com/edit/ng-zorro-antd-ivy-uda7th] (https://stackblitz.com/edit/ng-zorro-antd-ivy-uda7th)

steps to reproduce

Open the reproduction link directly, click the button, you should switch the Disabled status, but no

What is expected?

Click the button to Toggle Disabled status

What is actually happens?

Not reflected

ENVIRONMENT INFO
Ng-Zorro-ANTD 12.0.1
Browser Chrome

Our actual code is 12.0.1, but StackBlitz is a 11 version, but it is actually the same, and the same problem is that the STACKBLITZ link is the code of V11, and it is too much trouble. The following strategy is required in our code: ONPUSH strategy

  1. FormControlName
yangjunhan commented 2 years ago

Hi, @msyesyan. There are some mistakes made in your reproduction. A better way to manipulate formControl's disabled:

@Component({
  selector: 'nz-demo',
  template: `
    <form [formGroup]="formGroup">
      <button nz-button nzType="primary" (click)="toggle()"> Enable </button>
      <textarea nz-input formControlName="text"></textarea>
    </form>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class NzDemoInputBasicComponent {
  formGroup: FormGroup;
  private _disable = false;

  constructor(private cdr: ChangeDetectorRef) {
    this.formGroup = new FormGroup({
      text: new FormControl({ value: '', disabled: false })
    });
  }

  toggle(): void {
    if (this._disable) {
      this.formGroup.get('text')?.enable();
    } else {
      this.formGroup.get('text')?.disable();
    }
    this._disable = !this._disable;
    this.cdr.markForCheck();
  }
}
msyesyan commented 2 years ago

thanks,