Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $location get path before hash

Tags:

angularjs

I need to extract the path before the # in my AngularJS app, so for example if my app url is:

http://www.domain.com/folder/app/#/home

I need to get only this portion:

http://www.domain.com/folder/app/

I've tried using $location.path, $location.abspath but couldn't find any function that can get me the first portion before the #. Can someone please help me by telling me what I am missing here? and if there is a way to get the first portion of the url (before the #)? Thanks

like image 566
MChan Avatar asked Jun 16 '14 21:06

MChan


1 Answers

You could split the path into the pathname and hash parts, and then use the pathname only:

$location.absUrl().split('#')[0]
// "http://www.example.com/folder/app/"

split is just a Javascript string operation returning an array:

'http://www.example.com/folder/app/#/home'.split('#')
["http://www.example.com/folder/app/", "/home"]
like image 132
Matt Zeunert Avatar answered Sep 23 '22 14:09

Matt Zeunert