linkedQueryParam
- Two-way binding for query parameters
The linkedQueryParam
utility function creates a signal that is linked to a query parameter in the URL. This allows you to easily keep your Angular application’s state in sync with the URL, making it easier to share and bookmark specific views.
Key Features:
- Two-way binding: Changes to the signal are reflected in the URL, and changes to the URL are reflected in the signal.
- Parsing and stringification: You can provide functions to parse the query parameter value when reading it from the URL and stringify it when writing it to the URL.
- Built-in parsers: The library provides built-in parsers for common data types, such as numbers and booleans.
- Default values: You can provide a default value to be used if the query parameter is not present in the URL.
- Coalesced updates: Multiple updates to the signal within the same browser task are coalesced into a single URL update, improving performance.
- Supports Navigation Extras: The function supports navigation extras like
queryParamsHandling
,onSameUrlNavigation
,replaceUrl
, andskipLocationChange
. - Testable: The function is easy to test thanks to its reliance on Angular’s dependency injection system.
Usage
Here’s a basic example of how to use linkedQueryParam
:
In this example, the page signal is linked to the page query parameter. The parse function ensures that the value is always a number, defaulting to 1 if the parameter is not present or cannot be parsed. Clicking the “Next Page” button increments the page signal, which in turn updates the URL to /search?page=2
, /search?page=3
, and so on.
Parsing and Stringification
You can provide parse
and stringify
functions to transform the query parameter value between the URL and the signal. This is useful for handling different data types, such as booleans, numbers, and objects.
NOTE: Make sure to put the stringify
fn after the parse
fn in order for types to work correctly.
Default Values
You can provide a defaultValue
to be used if the query parameter is not present in the URL.
Note: You cannot use both defaultValue
and parse
at the same time. If you need to parse the value and provide a default, use the parse function to handle both cases.
Built-in Parsers
The ngxtension
library provides some built-in parsers for common data types:
paramToNumber
: Parses a number from a string.paramToBoolean
: Parses a boolean from a string.
Handling Null and Undefined Values
The linkedQueryParam
function handles null and undefined values gracefully.
If the query parameter is not present in the URL, the signal will be initialized with null.
You can also set the signal to null to remove the query parameter from the URL.
Navigation extras
These will work the same as using them on the navigate()
method of the Router.
queryParamsHandling
You can specify how to handle query parameters when updating the URL.
Options:
merge
(default): default behavior that will merge current params with new onespreserve
: won’t update to the new params''
: removes all other params except this one
Example usage:
skipLocationChange
When true, navigates without pushing a new state into history. If you want to navigate back to the previous query param using back button or browser back button, this will break that feature, because changes on the query params won’t be registered in the browser history.
replaceUrl
You can specify whether to replace the current URL in the browser’s history or push a new entry.
Examples of usage
- Usage with template driven forms & resource API