Given the following javascript:
$stateProvider
.state('search', {
url: '/search?query',
})
;
$urlRouterProvider.otherwise("search");
When I access the page
base_url?query=x
I get redirected to
base_url/search
but the query parameter gets lost.
Is there a way to pass the query parameter with the otherwise function?
To send parameters in URL, write all parameter key:value pairs to a dictionary and send them as params argument to any of the GET, POST, PUT, HEAD, DELETE or OPTIONS request. then https://somewebsite.com/?param1=value1¶m2=value2 would be our final url.
There is a working plunker
The UI-Router has native solution here.
The
otherwise
does not have to be the "url" string, it could be a function.
Check it in action in this Q & A:
How not to change url when show 404 error page with ui-router
The code could be like this:
$urlRouterProvider.otherwise(function($injector, $location){
var state = $injector.get('$state');
state.go("search", $location.search()); // here we get { query: ... }
return $location.path();
});
The $location.search()
will give us params object, which could be like: { query: ... }
. We take it and redirect to state search with this params...
Check it here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With