2

I would like to get my all url parts after "/". Example:

http://myadress.com/#/example/url/here/param1

Result: ['example', 'url', 'here'].

But $routeParams returns only object with route parameters {param1: 'param1'}

How can I do this ?

4 Answers 4

3

You can take url parts like this

var pathArray = window.location.pathname.split( '/' );

to get result from string.

var secondLevelLocation = pathArray[0];
Sign up to request clarification or add additional context in comments.

Comments

1

Another alternative to Jo_bast's answer would be to use hash rather than pathname as hash will start after the /#/

Both are correct, just different ways of using $window.location. Using hash, however, prevents you from having to deal with the path before the hash(#) in the url.

var path = $window.location.hash.split('/');
var path1 = path[1]; //example;
var path2 = path[2]; //url;

etc...

Comments

0

Remember that Angular is still JavaScript. You can use native js functions to do that.

Check out the location documentation.

You can use location.pathname to achieve what you want.

// http://myadress.com/#/example/url/here/param1
location.pathname.split('/') // --> ['', '#', 'example', 'url', 'here', 'param1']

Comments

0

You can use $location service which is part of angularjs:

var pathArray = $location.path().split('/');
//remove the first empty ''
pathArray.splice(0, 1);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.