0

I have to use parameter from url on the page.

For example, URL is smth like: url.com/index.html?username. I need to take this "username" show it on the page, and use it several times to get the right link.

<img src='http://url.com/project/{{---username here---}}/{{id}}.png' alt='' />

I've tried to do it with $location.search. But it doesn't work. It shows "cannot read property search of undefined" :(( Can you please tell me what I'm doing wrong and how should I do it in the right way?

Thanks a lot.

9
  • 1
    which routing you are using ngRoute or ui-router? Commented Jan 9, 2017 at 5:48
  • I think you should assing the value to $scope variable and then use + to concatnate it. like: in controller $scope.id = "A1B2C3"; and in the viwe <img src='url.com/project/'+{{id}}+'.png' alt='' /> Commented Jan 9, 2017 at 5:48
  • 1
    Do you inject $location service in your controller? From the error it's obvious that $location is not defined. Commented Jan 9, 2017 at 5:58
  • @AvneshShakya ngRoute Commented Jan 9, 2017 at 6:03
  • @MRB now I have the error like this one: $location is not defined :( Commented Jan 9, 2017 at 6:04

2 Answers 2

1

Use [**$location.search()**][1] to get the params as an object. Try after enabling the HTML5mode. In your app config, enable HTML5Mode like this:

$locationProvider.html5Mode(true)

and then ,

var params = $location.search();
Sign up to request clarification or add additional context in comments.

Comments

0

if you need an parameter from URL use it like this

// given URL http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}
searchObject['foo'] //  => 'bar'
searchObject.foo  //  => 'bar'

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.