$location.hash() returning empty string

3,410 views
Skip to first unread message

Lee Owen

unread,
Jan 2, 2013, 7:23:33 PM1/2/13
to ang...@googlegroups.com
Anchors aren't working in angular using ngViews.  If I log the result of $location.hash() in angular's function scroll() I see that it's getting a value of '' when the url is:  mydomain.com/#/test

Any ideas on why this might be occurring?

Joshua Miller

unread,
Jan 2, 2013, 7:25:50 PM1/2/13
to angular
Hi!

Can you provide a jsFiddle or Plunker so we can see your code in context? It'll make things a bit easier on us.

Josh


On Wed, Jan 2, 2013 at 4:23 PM, Lee Owen <itsle...@gmail.com> wrote:
Anchors aren't working in angular using ngViews.  If I log the result of $location.hash() in angular's function scroll() I see that it's getting a value of '' when the url is:  mydomain.com/#/test

Any ideas on why this might be occurring?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Lee Owen

unread,
Jan 2, 2013, 7:39:12 PM1/2/13
to ang...@googlegroups.com
I'm not sure how to get either jsFiddle or plunkr to allow hashtags.  This is the basic gist of it, yet in my project $location.path() shows the correct path, but $location.hash() is empty despite the path being "/#/whatever"

Lee Owen

unread,
Jan 2, 2013, 7:43:35 PM1/2/13
to ang...@googlegroups.com
if I log window.location.hash I get the hash result "#/test".  That is in my project, I can't seem to get any hash info from jsFiddle.

Joshua Miller

unread,
Jan 2, 2013, 7:52:48 PM1/2/13
to angular
There wasn't any active route URL in that example - it was only working on the `otherwise`. I changed it and now the route, which is the path, works as you'd expect: http://jsfiddle.net/joshdmiller/e3Bvp/2/ .

Josh


On Wed, Jan 2, 2013 at 4:43 PM, Lee Owen <itsle...@gmail.com> wrote:
if I log window.location.hash I get the hash result "#/test".  That is in my project, I can't seem to get any hash info from jsFiddle.

Lee Owen

unread,
Jan 2, 2013, 7:57:45 PM1/2/13
to ang...@googlegroups.com
Thanks Joshua!  Why isn't $location.hash() return "/test" while window.location.hash does show "#/test"?  This is where I'm confused.

Lee Owen

unread,
Jan 2, 2013, 8:01:20 PM1/2/13
to ang...@googlegroups.com
Additionally if I call $location.hash('test') the url ends up being mangled with double hash symbols as: "##test"

Lee Owen

unread,
Jan 2, 2013, 8:11:40 PM1/2/13
to ang...@googlegroups.com
I think that I understand my err.  I thought routes were based on a hash value such as "/#/my/route", but angular considers a route separate from a hash.  '#/my/route' is the path, and a hash would be appended to the end of that to look something like, "#/my/route#myHash".  Correct?

Joshua Miller

unread,
Jan 2, 2013, 10:19:39 PM1/2/13
to angular
Yep! You got it. Routes are based on the browser's hash, which means the `window.location.hash` contains whatever information is necessary to identify state in an SPA (say, for bookmarkability). Angular processes this information and makes it available through the $location service. So in order to use hashes as we would in something other than a single-page application, a second shebang is included, everything after which is a gettable/settable from $location.hash(). The guide has more info: http://docs.angularjs.org/guide/dev_guide.services.$location

Josh


On Wed, Jan 2, 2013 at 5:11 PM, Lee Owen <itsle...@gmail.com> wrote:
I think that I understand my err.  I thought routes were based on a hash value such as "/#/my/route", but angular considers a route separate from a hash.  '#/my/route' is the path, and a hash would be appended to the end of that to look something like, "#/my/route#myHash".  Correct?

--
Reply all
Reply to author
Forward
0 new messages