5

I have an url like this:

site.co/asd#antani?id=9

How do i get id value from this url dynamically and get the new value when it changes?

i tryed :

console.log($routeParams); and i got Object {} in console

thanks

3
  • @StarsSky lol you know it :D Commented Apr 5, 2014 at 12:41
  • Did you try $location.search(). it should return {id: 9} Commented Apr 5, 2014 at 12:52
  • @MaximShoustin i'm into a directive when i log $location.search() it's always empty object :( {} Commented Apr 5, 2014 at 12:53

5 Answers 5

2

Since I don't know your full Env. I would go to this dirtection:

var s = $location.search('site.co/asd#antani?id=9');

console.log(s.$$search);

Output:

Object {site.co/asd#antani?id: "9"} 

Hope it will help,

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

1 Comment

this is quite good solution rly but it redirects me to strange url like in a loop i dunno why :( lso the ? is re-encoded by browser or angular to %3F and i can't get why ufff :(
2

OK i think i fixed it and it was to change URL

so to make $routeParams works i needed to call

site.com/asd?id=9#antani

instead of

site.co/asd#antani?id=9

hope it can help also if i don't like the url in that way :D

big thanks to all and if you have any better solution let me know please !

2 Comments

Right, this is a proper form
yeah i don't like how it looks url but is the only right way i think
1

Got a gist to do that and other useful functions on urls with params ( URL params to object, Test if url Has Params, get Hash without params, and Change url params)

https://gist.github.com/dazzer13/10470514

You can do this:

var params = urlGetParams(url);

params.id // returns 9

Comments

0
var myURL = "site.co/asd#antani?id=9";
var myURL_id = myURL.split("#")[1];

1 Comment

thanks .. uhmm this quite basic i can parse it sure, i just would like to know if is out there some angular specifi method or way :P
0

Assuming this is the actual page you are on, you can use angular $routeParams. Something like this:

var id = $routeParams.id

9 Comments

sorry i forgot to say i already tryed routeParams but it always return empty Object {} in console :( i updated my question
Hmm...are you injecting it into a controller or service? The $routeParams variable doesn't exist on the global scope in the console, same as any other angular service. You need to use dependency injection (docs.angularjs.org/guide/di)
yeah ' injected both $location and $routeParams, but if i need some nested injection i'll avoid it i think anyway thanks :)
Not sure I understand what you mean by nested injection, but if you're injecting $routeParams and angular isn't throwing an error then it shouldn't be null inside that function. Can you print it using console.log or something?
console.log($routeParams); into directive returns Object {}
|

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.