0

I have a reactive form:

this.searchForm = this.formBuilder.group({
  query: [ null, [Validators.required] ]
});

And onPaste method where I update my form value, but actually it doesn't update HTML input value until I change the focus out of the input.

constructor(
    private formBuilder: FormBuilder,
    private ref: ChangeDetectorRef
  ) { }

onPaste(event: any): void {
    const pastedQuery = event.clipboardData.getData('text/plain');

    const formattedQuery = pastedQuery
      .split(/,?[\r\n\t]+\s?/).join(', ')
      .replace(/,\s?$/g, '');

    this.searchForm.patchValue({
      query: formattedQuery
    });

    this.ref.detectChanges();
  }

I call onPaste method like this:

<input formControlName="query" type="search" (paste)="onPaste($event)">

I've found this question Angular FormGroup won't update it's value immediately after patchValue or setValue and trying detectChanges but it doesn't help me.

So, where is my mistake, or maybe there is another way to solve my problem?

rel1x
  • 2,351
  • 4
  • 34
  • 62

0 Answers0