1

How can I get URI components in AngularJS? For example I have http://domain.com/photo/1

So how can I get that second segment (=1) to a variable in angular? Thank you.

Verba
  • 386
  • 2
  • 6
  • 16

1 Answers1

1

You don't need Angular for that, you can simply do:

var url = document.URL.split('/'); // Get the current full url, eg: http://domain.com/photo/1 splitted by slashes
var lastArg = url[url.length];     // Get the last string after the last slash
Andrei CACIO
  • 2,101
  • 15
  • 28
  • I got this kinda working with changing url[url.lenght] to url[url.lenght-1] but.. This only works if url is like http://domain.com/photo/1 but if there is a trailing slash after 1 it doesn't work. – Verba Mar 03 '15 at 19:39
  • If you want a full url access you can use `$routProvider` and access URL params like so: https://docs.angularjs.org/api/ngRoute/service/$routeParams – Andrei CACIO Mar 03 '15 at 19:57