I have a simple form:
this.searchForm = this.formBuilder.group({
query: [ null, [Validators.required] ]
});
When user paste something into the input I reformat values using regex and update the form.
onPaste(event) {
const formattedQuery = event.clipboardData.getData('text/plain')
.split(/,?[\r\n\t]+\s?/)
.join(', ')
.replace(/,\s?$/g, '');
this.searchForm.get('query').setValue(formattedQuery);
// this.ref.detectChanges();
}
But when I paste something, for example
325435956
325435956
It duplicate it and as a result I see 325435956, 325435956 325435956 325435956 but I expect to see 325435956, 325435956. Where is my mistake and how to fix that?
Working example you can find here https://stackblitz.com/edit/angular-cp9yhx?file=app%2Fhello.component.ts
this.searchForm.setValue({value:''})in theonPastemethod and try once. This will clear the form value whenever we are pasting, I guess the value is retained in the object