ixx
ixx

Reputation: 32269

Bookmarkable URL with search parameters?

I have some search functionality in my page, which uses GET requests and a JSON backend.

I want that if I run a search, I get an URL in the browser, with all the parameters, which the user can bookmark.

Let's say my search is:

search?key=a

I put this code in the success callback of the search, to update the URL:

$location.path("/search?key=a");

But then my URL gets updated to:

search#/search%3Fkey=a

If the user bookmarks this, of course it will not run the search, it will just show the search page with a blank input field.

I already did the setup to show the search results when the user has the correct link - I added this to the controller:

if (location.search) {
    $scope.callMyApi(location.search);
}

But when I'm using the links I get from AngularJS, like search#/search%3Fkey=a, $location.search() is empty.

$location.path(), on the other hand, has what I need:

/search?key=a

I could split this, and get the search string, and pass it to callMyApi. But it feels like I'm missing the Angular way to do it.

Already read the doc about $location and some articles about routing, but still don't know what I have to do.

Upvotes: 4

Views: 2440

Answers (1)

Chandermani
Chandermani

Reputation: 42669

You need to combine the location path method with search method. Something like

$location.path('/search').search({key:'a'});

Can you check documentation on how to use $location in developer guide.

Upvotes: 2

Related Questions