0

Im trying to accessing a value in a querystring.

Here's what i see in the browser:

http://localhost:1010/products/chairs.html?searchMe=testing123

And in my Controller, i have:

var mySearchMeQuery = $location.search().searchMe;

console.log("searchMe = " + mySearchMeQuery );

In my console.log, i am seeing: searchMe = undefined

I have checked i have $location injected in my controller.

** Solution must work in IE8+ **

3 Answers 3

1

$location works only if you use path with hash (#). See this.

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

Comments

1

Use $location.search()

It returns an object key-value pairs; The key is your query string and the value is the query string value.

Example:

function MainCtrl($scope, $location) {
    $scope.queryString = $location.search();
    // returns {searchMe: "testing123"}
}

And if you want to print the same message as in your question

 console.log("searchMe = " + $location.search().searchMe);

5 Comments

Thanks, what is the need for $window?
Yes, it will work. It works with query string, and doesn't depend on hashes.
$window is not needed.
See JanBarta's respsonse, $location only works with #
:) I'm sure it doesn't. Just try the code sample and see if it does.
0

You can inject dependecy $routeParams and get the parameter value using something like this:

$routeParams.searchMe

Docs on routeParams

UPDATED Answer:

I have updated an existing fiddle which had $routeParams as an example. If you click on Moby link and check console over there you can see that SearchMe as a querystring is available.

Fiddle

1 Comment

tried, var text = $routeParams.searchMe; console.log(text); the output was 'undefined'... Injected $routeParams into my controller.

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.