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

Bennett McElwee
  • 24,740
  • 6
  • 54
  • 63
itsme
  • 48,972
  • 96
  • 224
  • 345

5 Answers5

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,

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • 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 :( – itsme Apr 05 '14 at 12:59
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 !

itsme
  • 48,972
  • 96
  • 224
  • 345
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

Labithiotis
  • 3,519
  • 7
  • 27
  • 47
0
var myURL = "site.co/asd#antani?id=9";
var myURL_id = myURL.split("#")[1];
chris97ong
  • 6,870
  • 7
  • 32
  • 52
  • 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 – itsme Apr 05 '14 at 12:43
0

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

var id = $routeParams.id
alexsanford1
  • 3,587
  • 1
  • 19
  • 23
  • sorry i forgot to say i already tryed routeParams but it always return empty Object {} in console :( i updated my question – itsme Apr 05 '14 at 12:47
  • 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 (http://docs.angularjs.org/guide/di) – alexsanford1 Apr 05 '14 at 12:50
  • yeah ' injected both $location and $routeParams, but if i need some nested injection i'll avoid it i think anyway thanks :) – itsme Apr 05 '14 at 12:54
  • 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? – alexsanford1 Apr 05 '14 at 12:57
  • console.log($routeParams); into directive returns Object {} – itsme Apr 05 '14 at 13:00
  • Hmm...sounds like there is something weird happening. Might need some more clarification, maybe you could post your code? – alexsanford1 Apr 05 '14 at 13:02
  • i'll try but it's rly too much code to paste dack :D i'll try – itsme Apr 05 '14 at 13:05
  • Maybe put it in a plunkr? – alexsanford1 Apr 05 '14 at 13:08
  • yep for sure man i just need to cut off many many parts not needed :P – itsme Apr 05 '14 at 13:08