0

I am trying to get query strings to work in my Angular setup, but it is behaving weirdly.

When i go to a URL like this:

http://localhost:3000/?query=test

The URL changes and removes anything after the '=' to end up with:

http://localhost:3000/?query

Has anyone had this problem before?

I am currently using Angular UI Router with defined states and HTML 5 mode. I have also tried specifying the query parameter in the route as below:

.state('home', {
        url: '/?referrer',
        templateUrl: 'app/views/home/home.html',
        controller: 'mainController'
})

UPDATED........

OK. In my controller i had this:

var referrerURL = $location.search('referrer');

Removing this, fixes the issue. Why would this cause the problem?

5
  • It would be a lot easier to help if you provided some code. Are you using $location anywhere in your app? What about routing? Commented Dec 10, 2015 at 22:52
  • I've edited my question. Sorry about that. I am also using $location yes injected into certain controllers, but only being used to grab certain segments of the URL on certain pages. Commented Dec 10, 2015 at 22:59
  • Read the fine manual ~ docs.angularjs.org/api/ng/service/$location#search ~ "Change search part when called with parameter". It's only a getter when you call it with no arguments Commented Dec 10, 2015 at 23:05
  • Also, seeing as it's part of your state configuration, why not just use var referrerURL = $stateParams.referrer? Commented Dec 10, 2015 at 23:43
  • Check that the state variables in your $scope before and after your call that you deleted are still the same. Commented Dec 10, 2015 at 23:43

2 Answers 2

1

For $location.search() method, you should use it like this:

$location.search('key','value').

If you don't set the value, it would be "true" by default.

More information here

Sign up to request clarification or add additional context in comments.

1 Comment

I think OP is attempting to retrieve the value, not set it so it should be var referrerURL = $location.search().referrer or even more simply $stateParams.referrer
0

Thanks for all your input.

I fixed this by changing my controller to use:

var referrerURL = $location.search().referrer;

Comments

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.