From efd9d624770a0f526267df7bfdf256e967c2f970 Mon Sep 17 00:00:00 2001 From: genja Date: Mon, 8 Jun 2026 15:17:39 +0200 Subject: [PATCH] fixed FormControl <-> Query Params binding --- .../app-searchbar/app-searchbar.component.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/webapp/src/app/shared/ui/app-searchbar/app-searchbar.component.ts b/webapp/src/app/shared/ui/app-searchbar/app-searchbar.component.ts index 711c0211..3196c3b5 100644 --- a/webapp/src/app/shared/ui/app-searchbar/app-searchbar.component.ts +++ b/webapp/src/app/shared/ui/app-searchbar/app-searchbar.component.ts @@ -1,4 +1,4 @@ -import { Component, inject, ViewChild, DestroyRef} from '@angular/core'; +import { Component, inject, ViewChild, DestroyRef } from '@angular/core'; import { MatIcon } from '@angular/material/icon'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { Router, ActivatedRoute } from '@angular/router'; @@ -43,11 +43,19 @@ export class AppSearchbarComponent { private readonly router = inject(Router); private readonly route = inject(ActivatedRoute); private readonly personService = inject(PersonsService); - constructor() { - const params = new URLSearchParams(window.location.search); - const currentSearch = params.get('query') ?? ''; - this.searchControl.setValue(currentSearch, { emitEvent: false }); + const route = inject(ActivatedRoute); + const router = inject(Router); + + // Sync searchControl from query param on every URL change + route.queryParams + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe((params) => { + const query = params['query'] ?? ''; + if (this.searchControl.value !== query) { + this.searchControl.setValue(query, { emitEvent: false }); + } + }); // Autocomplete suggestions this.searchControl.valueChanges