Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass the query parameter with the othwerwise function

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?

like image 996
David Michael Gang Avatar asked Apr 29 '15 14:04

David Michael Gang


People also ask

How do you pass query parameters in Python?

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&param2=value2 would be our final url.


1 Answers

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

like image 162
Radim Köhler Avatar answered Sep 18 '22 12:09

Radim Köhler