Forget about using injector and $state.go, it's unnecessary. The otherwise
method can take a URL path as an argument, allowing you to include parameters within it.
In this specific scenario, the following code will redirect you to base_url/search?query=x
$urlRouterProvider.otherwise("/search?query=x");
Alternatively, you can pass a function as the argument that generates a dynamic URL path based on certain criteria. If unsure about the parameters, you can extract them from $location
, format them into a URL-like string, and return the result.
$urlRouterProvider.otherwise(function($injector, $location) {
var params = $location.search()
// convert params to 'p1=v1&p2=v2' format
var formatted = Object.keys(params).map(function(key) {
return key + '=' + params[key]
}).join('&')
return '/search?' + formatted
});