I have a simple Search Component which contains a Reactive Form with 2 elements:
So far I use myFormGroup.valueChanges.subscribe(...)
to execute my search method.
Now the problem is, that I want to debounce the text input. And at the same time not debounce the checkbox, so the search method is getting executed instantly when clicking the checkbox.
Using valueChanges.debounceTime(500)
will of course debounce the whole form. That's not what I want.
This is a stripped down example. The real form has some more inputs. Some should be debounced and some shouldn't.
Is there any easy way to get this done? Or do I have to subscribe to every form control separately?
Would be nice to see how you did solve this.
Thanks in advance.
export class SearchComponent {
myFormGroup: FormGroup;
constructor(fb: FormBuilder) {
this.myFormGroup = fb.group({
textInput: '',
checkbox: false
});
}
ngOnInit() {
this.myFormGroup.valueChanges.subscribe(val => {
// debounce only the textInput,
// and then execute search
});
}
}
Create each individual FormControl before adding them to the form group and then you can control the valueChanges observable per FormControl
this.textInput.valueChanges
.pipe(
debounceTime(400),
distinctUntilChanged()
)
.subscribe(res=> {
console.log(`debounced text input value ${res}`);
});
the distinctUntilChanged will make sure only when the value is diffrent to emit something.