1

I want to keep the query string while navigating:

http://localhost/#/test/page1?param=123

My navigation links are as follows:

<a ng-href="test/page2>

When I click the link, the url changes to: http://localhost/#/test/page2 and the query string is removed.

Question: how can I keep the query?

6
  • 1
    What's the problem to use <a ng-href="test/page1?param=123">? Commented Sep 22, 2015 at 9:02
  • I get the query from user input, I do not define the query myself. Thus I have to preserve it,. Commented Sep 22, 2015 at 9:06
  • You can use $locationChangeStart callback and add querystring to URL Commented Sep 22, 2015 at 9:07
  • Is it must to keep query string in url? instead use a service to keep it in ngcookie and retrive back on next page Commented Sep 22, 2015 at 9:09
  • It's a must to keep the url for user feedback. Commented Sep 22, 2015 at 9:19

2 Answers 2

1

If you are happy to not use ng-href you can do this very cleanly and simply using $location.path() like this:

In your controller declare a function that ultimately does this:

$scope.sendToNewPlace = function (path) {
    $location.path(path);
}

(don't forget to inject $location into your controller (or where-ever you ultimately put the $location.path() call).

Then change your links to use ng-click like this:

<a href="#" ng-click="sendToNewPlace('test/page2');">page2, passing querystring</a>
Sign up to request clarification or add additional context in comments.

Comments

0
$scope.$on('$locationChangeStart', function(event, next, current) {
//next: holds the new Url
//current: holds the old Url
});

For more Reference : https://docs.angularjs.org/api/ng/service/$location

2 Comments

So and how can I change the target url? I tried the following, which changes the next variable, but the browser url is not changed! next = next + "?" + current.split('?')[1]
function(string1) { string1 = string1 + "anything"} will never change 'string1' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.