簡體   English   中英

如何通過 Angular 9 中的 FormGroup (AbstractControl) 在 Reactive Form 中重置 ng-select?

[英]How to reset ng-select in Reactive Form through a FormGroup (AbstractControl) in Angular 9?

我有一個帶有一堆 ng-select 元素的 FormGroup。 我還有以下代碼可以重置反應表單中的所有值,但 ng-select 元素除外。

  Object.keys(this.myFormGroup.controls).forEach(key => {
    const currentControl = this.myFormGroup.controls[key];
    if (currentControl == null) {
      currentControl.reset();
    }
  });

我也嘗試了currentControl.patchValue('')null ,但它不起作用。 當我在ng-select中加載錯誤的“ item ”時,它有一個選定的值“ undefined ”,我想在它undefined時清除此默認選定值。 這就是我使用== null的原因。

即使您有一個 hacky JavaScript (Vanilla) 解決方案,也請分享它。

您是否嘗試傳遞要將 ng-select 重置為的初始值; 里面的reset function 像這樣嗎?

    Object.keys(this.myFormGroup.controls).forEach(key => {
    const currentControl = this.myFormGroup.controls[key];
    if (currentControl == (null || undefined)) {
      currentControl.reset('');
    }
  });

我還添加了對未定義控制值的檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM