简体   繁体   English

<ng-select>在更改表单组控制值后仍然 null,如果 searchTerm 以编程方式提供</ng-select>

[英]<ng-select > after on changes formGroup control value still null,if searchTerm provided programmatically

I have requirement to set searchTerm value Programmatically(using Virtual keypad) and search the dropdown should show searchterm based options.我需要以编程方式设置 searchTerm 值(使用虚拟键盘)并搜索下拉菜单应显示基于 searchterm 的选项。

where form control is updateOn:'blur'.其中表单控件是 updateOn:'blur'。

But here i am facing issue like form control is not getting updated after selecting dropdown option based on searchTerm provided by virtual input key(in example stackblitz red color text control value is null).但是在这里我面临的问题是,在基于虚拟输入键提供的 searchTerm 选择下拉选项后,表单控件没有得到更新(例如stackblitz红色文本控件值为空)。

Steps:脚步:

1.press virtual key pad button 1.按虚拟键盘按钮

2.select any option 2.select 任意选项

3.now check red color text value: 3.现在检查红色文本值:

Thanks.谢谢。

It is getting updated, but you are using before it is updated.正在更新,但您在更新之前正在使用它。 Even if you open the <ng-select> yourself, it will show different values in black and red.即使您自己打开<ng-select> ,它也会以黑色和红色显示不同的值。

If you can , I'd suggest for you to use change.id instead:如果可以的话,我建议您改用change.id

    this.afterChangesFormControlValue = change.id;

But if you can't... if you wrap it inside a setTimeout() , even with a 0 delay, it will start working, since the setTimeout will be triggered only after the update is done:但是如果你不能......如果你将它包装在setTimeout()中,即使延迟为0 ,它也会开始工作,因为 setTimeout 只会在更新完成后触发:

  setTimeout(() => {
    console.log('on select option after given search term from virtual keyboar')
    console.log("change:",change)
    console.log("form control value:",this.form.controls.example.value)
    this.afterChangesFormControlValue=this.form.controls.example.value;
  }, 0);

Both should produce the same results - they did when I tested in your Stackblitz, but setTimeout is a bit unsightly.两者都应该产生相同的结果 - 当我在您的 Stackblitz 中进行测试时它们会产生相同的结果,但是setTimeout有点难看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM