仅针对用户输入进行角度去抖

Mat*_*ník 3 debouncing angular angular-reactive-forms

我在 Angular2 中有一个反应式表单设置。在组件中,我订阅表单控件的值更改,防抖时间为 500 毫秒,如下所示:

myForm.get("myField").valueChanges.debounceTime(500).subscribe(...);
Run Code Online (Sandbox Code Playgroud)

如果值是由代码而不是用户更改的,是否有办法跳过去抖时间?或者有没有办法将这两个事件分开?

AJT*_*T82 5

在以编程方式设置值的地方,您可以使用emitEvent:false,在文档中说明...

如果emitEventtrue,则此更改将导致valueChanges在 FormControl 上发出事件。这默认为true.

因此将其设置为false不会导致valueChanges火灾,因此如果您在某个时刻设置该值,可以执行以下操作:

this.myForm.get('myField').patchValue('my value', {emitEvent:false})
Run Code Online (Sandbox Code Playgroud)