[英]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.