Is there a way to seamlessly transform URL parameters? Let me illustrate with a scenario. Our URL structure is like this:
/shopping/nuts/:productId
/shopping/berries/:productId
/shopping/juice/:productId
The products in our app, fetched from an API, may look like this:
{ type: 'berry', price: 123, text: 'lorem ipsum', id: '12345' }
There's a discrepancy between the plural and singular forms: The URL uses the plural form (e.g. 'berries'), while the API delivers the singular form ('berry').
A portion of our state definition appears as follows:
.state('shop.product', {
url: '/shopping/:type/:productId',
templateUrl: 'type.html',
controller: 'TypeController'
})
Issue: I have to call a function in every controller to transform the $state
parameter into singular form (toSingular($stateParams.type)
). And when creating links, I need to do the reverse transformation. This process is tedious and error-prone.
Therefore, my ideal solution would be:
For URL /shopping/berries/12345
, $stateParams.type === 'berry'
. When generating the URL using
ui-sref="shop.product({type: 'berry', id: '12345'})"
, it should return /shopping/berries/12345
.
I've been struggling to find a suitable integration point for this logic. Any feedback is highly appreciated!