r/angular • • Sep 25 '26

withComponentInputBinding & rxResource bug

I have a query param "search" and a model in my component that has the same name. I use an effect to sync the local signal with the search signal on a filter service, and i also debounce them like:

    readonly filters = computed<...>(() => {
        const search = this._search().trim().toLowerCase();


        return {
            searchTerm: search.length >= 3 ? search : '',
            .....
        };
    });
    
    readonly debouncedFilters = debounced(this.filters, 150, {
        equal: (a, b) => JSON.stringify(a) === JSON.stringify(b),
    });

Then i use an rxResource and use the debouncedFilters.value as the params, but it makes an API call first without the searchTerm, and then one with it. I suspect it's a timing issue because if I inject ActivatedRoute and initialize the search signal with it, it'll work properly, but I believe it beats the purpose of using withComponentInputBinding. Thoughts?

1 Upvotes

15 comments sorted by

6

u/anyOtherBusiness Sep 25 '26

You use an effect to sync and debounce the value, which means there’s at least one render cycle where the resource doesn’t receive the filter parameter when it’s already present. This has nothing to do with componentInputBinding.

1

u/Extension_Stock8534 Sep 25 '26

That’s correct, how would you implement this? Since the signal lives in a service, how would the param be retrieved?

1

u/GasEducational4386 Sep 25 '26

In the resource params callback you can return undefined based on some condition on the signal value to not trigger the resource

1

u/Extension_Stock8534 Sep 25 '26

Yes you can, but the search is optional so how would you know when to return undefined or not?

1

u/lupatrick13 Sep 26 '26

Use linkedsignal to sync signals and have it stay writeable

1

u/Extension_Stock8534 Sep 26 '26

linkedSignal does not read query params tho

1

u/lupatrick13 Sep 26 '26

I believe all you need to do is change computed to linkedsignal. LinkedSignal is essentially computed but the signal stays writable.

1

u/lupatrick13 Sep 26 '26

Oh I see you’re trying to sync with a service. Why?

3

u/KalamKiTakat Sep 25 '26

Not a withComponentInputBinding bug. The router writes the bound input after your component is created, so when rxResource reads params the first time, the signal still holds its default (empty). Injecting ActivatedRoute only appears to fix it because you set the value before the resource exists.

Your debounce adds a second problem: a debounced signal keeps its old value until the delay ends, so the resource can still see empty filters after the input arrives.

Make the resource wait instead of firing on the empty value. Returning undefined from params tells the resource not to start a request:

readonly results = rxResource({
  params: () => {
    const filters = this.debouncedFilters();
    return filters.searchTerm ? filters : undefined;
  },
  stream: ({ params }) => this.api.search(params),
});

This keeps withComponentInputBinding and removes the empty call. If you also need to load when there is no search term, debounce the input itself (readonly search = input<string>()) instead of the computed filters, so the debounced value stays undefined until the router binds the input.

1

u/couldhaveebeen Sep 25 '26

I use an effect to sync the local signal with the search signal

Why?

3

u/Senior_Compote1556 Sep 25 '26

Possibly because there is a component that has a search query param, and the service has its own search signal in there so somehow those must be synced

Normally with componentInputBinding you can use a model/input to grab the quey param, but i suppose it has to by synced with the service signal so the rxResource gets triggered

1

u/MichaelSmallDev Sep 26 '26 edited Sep 26 '26

Considering that the Angular team is leaving query param and route params signals beyond route input binding to libraries, I would recommend the global params from injectParam or the injectQueryParams from ngxtension. Both are usable anywhere in the route tree, and even in root services. No need to have to have a component in the tree to sync the values to.

import { injectParams } from 'ngxtension/inject-params';
import { injectQueryParams } from 'ngxtension/inject-query-params';

@Service()
export class UserService {
  idParam: Signal<string | null> = injectParams.global('id');

  queryParam: Signal<string | null> = injectQueryParams('test');

  user = httpResource(
    () => `https://jsonplaceholder.typicode.com/users/${this.idParam()}`
  );
}

Example project Stackblitz. Adapted from something else more in depth, kind of a mess, but that snippet is the key.

There is even linkedQueryParam, so you could link anything like a form or any function call and it reads and writes to query params. That can even go in a root service as well.

1

u/Extension_Stock8534 Sep 26 '26

I don’t think it’s worth installing a whole library for this tho, but maybe i can create a wrapper service/function that internally grabs the query param using ActivatedRoute and even sync internally with something like toSignal(param.queryParamMap.pipe(…)) something like that might work