[英]How to reset ng-select in Reactive Form through a FormGroup (AbstractControl) in Angular 9?
I have a FormGroup with a bunch of ng-select elements.我有一个带有一堆 ng-select 元素的 FormGroup。 I also have the following code that resets all of the values in the Reactive Form except the ng-select elements.
我还有以下代码可以重置反应表单中的所有值,但 ng-select 元素除外。
Object.keys(this.myFormGroup.controls).forEach(key => {
const currentControl = this.myFormGroup.controls[key];
if (currentControl == null) {
currentControl.reset();
}
});
I tried also currentControl.patchValue('')
and with null
, but it doesn't work.我也尝试了
currentControl.patchValue('')
和null
,但它不起作用。 When I load a wrong " item
" in the ng-select
it has a selected value " undefined
" and I want to clear this default selected value when it is undefined
.当我在
ng-select
中加载错误的“ item
”时,它有一个选定的值“ undefined
”,我想在它undefined
时清除此默认选定值。 That's why I use == null
.这就是我使用
== null
的原因。
Even if you have a hacky JavaScript (Vanilla) solution, share it.即使您有一个 hacky JavaScript (Vanilla) 解决方案,也请分享它。
Did you try to pass the initial value you want to reset the ng-select to;您是否尝试传递要将 ng-select 重置为的初始值; inside the reset function like so?
里面的reset function 像这样吗?
Object.keys(this.myFormGroup.controls).forEach(key => {
const currentControl = this.myFormGroup.controls[key];
if (currentControl == (null || undefined)) {
currentControl.reset('');
}
});
I also added check for undefined control value too.我还添加了对未定义控制值的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.